简体   繁体   English

Python Selenium XPath追溯错误

[英]Python Selenium XPath Traceback Error

I am getting an error with selenium's find_element_by_xpath. 我遇到硒的find_element_by_xpath错误。 This is the error I'm getting: 这是我得到的错误:

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)

Here is the piece of code that the error is pointing to: 这是错误所指向的代码:

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()

I don't know what is going on? 我不知道怎么回事? I am trying to make a code that gets the first term and definition from the list. 我正在尝试编写一个从列表中获取第一个术语和定义的代码。 Then it clicks on both of them on the page and goes to the next term. 然后,它在页面上都单击它们,然后转到下一个学期。 I want to find the element by the text between the tags ( term name ) 我想通过标签(术语名称)之间的文本查找元素

Thanks in advance. 提前致谢。

If you are using contains(), provide the bare minimum to ensure a unique text match. 如果您使用contains(),请提供最低限度的内容以确保唯一的文本匹配。 Hint: any extraneous characters can bite you. 提示:任何无关紧要的字符都会咬你。

Try your locator out in the browser using the console in the dev tools. 使用开发工具中的控制台在浏览器中尝试定位器。 $$("css selector here") or $x("xpath here"). $$(“ css选择器在这里”)或$ x(“ xpath在这里”)。 If it returns your element, you're locked and loaded. 如果返回您的元素,则您将被锁定并加载。 If not, adapt and overcome. 如果没有,请适应并克服。

Here is another useful method of troubleshooting: 这是另一种有用的故障排除方法:

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

It will drop you into your script just before the click, with all the variables intact. 单击之前,它将带您进入脚本,所有变量均保持不变。 Try running element.click() manually, and watching what happens on your page. 尝试手动运行element.click() ,然后观察页面上发生的情况。 Clicks can be finicky, you may have to try clicking with offset, or in extreme cases executing a javascript snipped to click (I've found this is rare and a last resort). 点击可能是挑剔的,您可能必须尝试使用​​偏移量进行点击,或者在极端情况下执行被截断的javascript来点击(我发现这种情况很少见,并且是不得已的做法)。

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

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