我尝试使用硒的webdriver找到一个元素: i want click the "用户查询"硒的webdriver定位元素
对应的HTML: the red frame
蟒蛇:
>>> driver.find_element_by_xpath("//div[@id='container']/h1[2]/a").click()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
driver.find_element_by_xpath("//div[@id='container']/h1[2]/a").click()
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "D:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element
(Session info: chrome=52.0.2743.116)
(Driver info: chromedriver=2.7.236900,platform=Windows NT 6.1 SP1 x86)
如何定位这个元素?
谢谢你你的答案真诚。我尝试它,并改变为“driver.find_element_by_xpath(”// * [text()='用户查询']“)”他们都没有working.any选择? –
当我们从浏览器中看到'html'时,您的文本'用户查询'正在发生变化。我也更新了我的答案。假设你的文本'用户查询'位于html内部,并带有'n(数字)',那么使用xpath作为'// h1 [n]/a' –
是的,我已经尝试过了。在网站上传和切换后,一帧。非常感谢你。 –