这里有什么区别阻止了它的工作? [英] What is the difference here that prevents this from working?

查看:33
本文介绍了这里有什么区别阻止了它的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读客户姓名列表并使用每个名称来查找元素.

I'm reading a list of customer names and using each to find an element.

在阅读列表之前,我可以在硬编码名称时确认这是有效的,

Before reading the list, I make can confirm this works when I hard-code the name,

    datarow = driver.find_element_by_xpath("//span[contains(text(),'ACME Anvil Company')]")

但是当我读入客户列表并像这样使用它时,我得到一个 NoSuchElement 异常.我知道我将名称输入到客户变量中,因为打印语句确认了这一点.

But when I read in the customer list and use it like this, I get a NoSuchElement exception. I know I'm getting the name into the customer variable because the print statement confirms it.

for customer in customerlist:
    print("START OF DATA FOR CUSTOMER: " +customer)
    datarow = driver.find_element_by_xpath("//span[contains(text(),'"+customer+"')]")

我对"有什么问题吗?+客户+ "'部分?我已经尝试了很多不同的方法.

Do I have something wrong with the '" +customer+ "' part? I've tried it a bunch of different ways.

推荐答案

可能是列表元素,例如customer,包括前导或尾随空格.因此,当您通过 print() 语句打印时,您正在监督这些.

Possibly the list elements e.g. customer, includes leading or trailing white spaces. So when you print through print() statement you are overseeing those.

但是当您使用 时:

datarow = driver.find_element_by_xpath("//span[contains(text(),'"+customer+"')]")

那些空格开始起作用,但没有找到匹配项.

Those whitespaces comes into play and no matches are found.

您可以使用以下解决方案:

You can use the following solution:

datarow = driver.find_element_by_xpath("//span[contains(.,'"+customer+"')]")

理想情况下,要找到您需要诱导 WebDriverWait 的元素对于 visibility_of_element_located(),您可以使用以下 定位器策略:

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategy:

datarow = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(.,'"+customer+"')]")))

这篇关于这里有什么区别阻止了它的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆