我想在3步骤对话框中识别按钮,所以需要在前两步中点击相同的“下一步”按钮对话框中,当用于第一步骤中,其正常工作的xpath相同,但在步骤失败2.selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素
我的Python代码:
a=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
a.send_keys(selenium.webdriver.common.keys.Keys.SPACE)
上面的代码作品和元件被点击。
driver.implicitly_wait(30)
b=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
b.send_keys(selenium.webdriver.common.keys.Keys.SPACE)
这会失败,出现以下异常。
异常观察:
Traceback (most recent call last):
File "C:\Users\sabarish.kannan\workspace1\de.vogella.python.first\src\FirstModule.py", line 49, in <module>
b=driver.find_element_by_xpath(".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]")
File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
'value': value})['value']
File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "C:\Users\sabarish.kannan\AppData\Local\Programs\Python\Python36\lib\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":".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)
请分享URL或HTML – iamsankalp89
也许在首次按钮点击触发的JS代码,更改HTML 。您使用的绝对路径在HTML更改时可能会失败。 – nutmeg64
请仔细阅读[问],特别是关于[mcve](MCVE)的部分,以及[预计需要多少研究工作?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort这是预期的堆栈溢出用户)这将帮助您调试自己的程序并为自己解决问题。如果你这样做并且仍然卡住,你可以回过头来发布你的MCVE,你试过的,以及执行结果,包括任何错误信息,这样我们可以更好地帮助你。还提供了一个链接到页面和/或相关的HTML。 – JeffC