Python SeleniumWebDriver定位元素
我尝试使用selenium webdriver查找元素: 对应的html: python:Python SeleniumWebDriver定位元素,python,selenium-webdriver,Python,Selenium Webdriver,我尝试使用selenium webdriver查找元素: 对应的html: python: >>> 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_x
>>> 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)
>>驱动程序。通过xpath(“//div[@id='container']/h1[2]/a”)查找元素。单击()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
驱动程序。通过xpath(“//div[@id='container']/h1[2]/a”)查找元素。单击()
文件“D:\Python35\lib\site packages\selenium\webdriver\remote\webdriver.py”,第293行,位于find\u element\u by\u xpath中
返回self.find_元素(by=by.XPATH,value=XPATH)
文件“D:\Python35\lib\site packages\selenium\webdriver\remote\webdriver.py”,第752行,位于find\u元素中
'value':value})['value']
文件“D:\Python35\lib\site packages\selenium\webdriver\remote\webdriver.py”,第236行,在execute中
self.error\u handler.check\u响应(响应)
文件“D:\Python35\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第192行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素
(会话信息:chrome=52.0.2743.116)
(驱动程序信息:chromedriver=2.7.236900,平台=Windows NT 6.1 SP1 x86)
如何定位该元素?您可以尝试定位该元素:
您可以尝试定位元素:
尝试下面的xpath索引 在这里。[1] 指示
标记的索引号。假设在您的html中,有许多标签可以使用如下不同的名称<代码>用户查询代码>。假设,此文本用户查询代码>索引编号是html中的编号,然后使用下面的xpath
//h1[1]/a
//h1[2]/a
如果此文本用户查询代码>索引号在html
中为2,然后使用下面的xpath
//h1[1]/a
//h1[2]/a
尝试下面的xpath索引
在这里。[1] 指示标记的索引号。假设在您的html中,有许多标签可以使用如下不同的名称<代码>用户查询代码>。假设,此文本用户查询代码>索引编号是html中的编号,然后使用下面的xpath
//h1[1]/a
//h1[2]/a
如果此文本用户查询代码>索引号在html
中为2,然后使用下面的xpath
//h1[1]/a
//h1[2]/a
谢谢你的回答。我尝试了一下,但是没有这样的错误元素。我认为你是对的,这个元素是一个带有文本的href,但是没有一个是有效的,有什么想法吗?你能分享整个页面的HTML吗?可能是
标签在
内,或者是一些使元素更难定位的东西?我很抱歉最近回复你,我的vpn被破坏了,我看不到这个网站。是我的错,我忘了提到它有两个iframe。sulotion是驱动程序。切换到iframe(“xx”)谢谢你的回答。我尝试了,但是错误没有这样的元素。我想你是对的,这个元素是一个带有文本的href,但是没有一个是有效的,有什么想法吗?你能分享整个页面的HTML吗?可能是
标签在
内,或者是一些使元素更难定位的东西?我很抱歉最近回复你,我的vpn被破坏了,我看不到这个网站。是我的错,我忘了提到它有两个iframe。sulotion是驱动程序。切换到\u frame(“xx”)衷心感谢你的回答。我尝试了,并更改为“driver.find_element_by_xpath”(“/*[text()=”的缩写)用户查询']“”“它们都不起作用。有选择吗?您的文本用户查询当我们从浏览器中看到html
时,code>正在发生变化。我也更新了我的ans。假设您的文本用户查询
位于html中,带有n(数字)
然后使用xpath作为//h1[n]/a
是的,我已经尝试过了。网站上传后,您完全正确,并切换到了框架。非常感谢您。衷心感谢您的回答。我尝试过,并改为“驱动程序。通过xpath查找元素”(//*[text()=”用户查询']“”“它们都不起作用。有选择吗?您的文本用户查询当我们从浏览器中看到html
时,code>正在发生变化。我也更新了我的ans。假设您的文本用户查询
位于html中,带有n(数字)
然后使用xpath作为//h1[n]/a
是的,我已经尝试过了。在网站上传并切换到框架后,您完全正确。非常感谢。