Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这里的区别是什么阻止了它的工作?_Python_Selenium_Xpath_Webdriverwait_Expected Condition - Fatal编程技术网

Python 这里的区别是什么阻止了它的工作?

Python 这里的区别是什么阻止了它的工作?,python,selenium,xpath,webdriverwait,expected-condition,Python,Selenium,Xpath,Webdriverwait,Expected Condition,我正在阅读一个客户名称列表,并使用每个名称查找一个元素 在阅读列表之前,我可以在硬编码名称时确认这一点 datarow = driver.find_element_by_xpath("//span[contains(text(),'ACME Anvil Company')]") 但是当我阅读客户列表并像这样使用它时,我得到了一个NoTouchElement异常。我知道我正在将名称输入到customer变量中,因为print语句确认了它 for customer in

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

在阅读列表之前,我可以在硬编码名称时确认这一点

    datarow = driver.find_element_by_xpath("//span[contains(text(),'ACME Anvil Company')]")
但是当我阅读客户列表并像这样使用它时,我得到了一个NoTouchElement异常。我知道我正在将名称输入到customer变量中,因为print语句确认了它

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

“客户+”部分是否有问题?我尝试过很多不同的方法。

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

但当您使用as时:

这些空白开始起作用,但未找到匹配项


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

datarow = driver.find_element_by_xpath("//span[contains(.,'"+customer+"')]")
理想情况下,为了定位元素,您需要为元素(located()的可见性引入元素,您可以使用以下方法:


客户列表是如何创建的?例如,文本中可能有尾随空格或换行符-打印变量的
repr()
,以准确查看其中的内容。您的
customerlist
看起来像什么?您是否检查过它是否与您的硬编码字符串完全相同?(我的意思是
==
-选中,仅打印是不够的)@jasonharper我只是通过在文本文件中输入姓名来创建客户列表。。。。。键入名称,按enter键,键入名称,按enter键。。。。所以有一个角色我没看到是有道理的。
datarow = driver.find_element_by_xpath("//span[contains(.,'"+customer+"')]")
datarow = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(.,'"+customer+"')]")))