繁体   English   中英

Python Selenium XPath追溯错误

[英]Python Selenium XPath Traceback Error

我遇到硒的find_element_by_xpath错误。 这是我得到的错误:

Traceback (most recent call last):
  File "/Users/iyadschool/Desktop/q_hack.py", line 15, in <module>
    text = driver.find_element_by_xpath("//*[contains(text(), ' " + term[1] + "')]")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element
    'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(text(), ' indifference')]"}
  (Session info: chrome=64.0.3282.186)
  (Driver info: chromedriver=2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef),platform=Mac OS X 10.12.6 x86_64)

这是错误所指向的代码:

term = ["impalpable", "indifference", "complacent", "transpire", "abject", "derivative", "derive", "dispel", "authentic", "deprived", "aesthetic", "affinity", "depict", "impertinent", "inevitable", "deviate", "aggrandize", "aggregate"]
definition = ["unable to be felt", "lack of concern", "self-satisfied", "to happen", "miserable", "unoriginal", "to get\
come from", "to drive away", "real", "take away something", "pertaining to beauty", "a natural attraction or feeling of kinship", "To give a picture of; to describe", "rude and disrespectful", "unavoidable", "depart, from a path or plan", "to increase in power, influence, and reputation", "(v.) - to gather, amass"]

termnumb = len(term)
defnumb = len(definition)

text = driver.find_element_by_xpath("//*[contains(text(), ' " + term[1] + "')]")
text.click()

我不知道怎么回事? 我正在尝试编写一个从列表中获取第一个术语和定义的代码。 然后,它在页面上都单击它们,然后转到下一个学期。 我想通过标签(术语名称)之间的文本查找元素

提前致谢。

如果您使用contains(),请提供最低限度的内容以确保唯一的文本匹配。 提示:任何无关紧要的字符都会咬你。

使用开发工具中的控制台在浏览器中尝试定位器。 $$(“ css选择器在这里”)或$ x(“ xpath在这里”)。 如果返回您的元素,则您将被锁定并加载。 如果没有,请适应并克服。

这是另一种有用的故障排除方法:

elem = driver.find_element_by_xpath("//div[contains(text(), 'derive')]")
import pdb; pdb.set_trace()
text.click()

单击之前,它将带您进入脚本,所有变量均保持不变。 尝试手动运行element.click() ,然后观察页面上发生的情况。 点击可能是挑剔的,您可能必须尝试使用​​偏移量进行点击,或者在极端情况下执行被截断的javascript来点击(我发现这种情况很少见,并且是不得已的做法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM