【发布时间】:2021-09-18 23:09:37
【问题描述】:
我正在尝试使用 selenium 导入元掩码,但是当我使用 xpath /html/body/div[1]/div/div[3]/div/div/div/button 时,我的控制台返回“selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath ","选择器":"/html/body/div[1]/div/div[3]/div/div/div/button"}
(会话信息: chrome=93.0.4577.82)"
奇怪的是,当我在 chrome 开发工具中使用 $x("/html/body/div[1]/div/div[3]/div/div/div/button") 时,它能够识别“开始”按钮。我该如何解决这个错误?为什么会出现这个错误?
完整源代码:
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_extension('metamask-chrome-9.8.4.crx')
driver = webdriver.Chrome('./chromedriver', options = options)
driver.get('https://google.com')
time.sleep(2)
get_started_button = driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div/div/div/button")
get_started_button.click()
input('Press [ENTER] to close browsers...')
driver.quit()```
完整的错误日志:
File "D:\Rias\metamask selenium\script.py", line 13, in <module>
get_started_button = driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/div/div/div/button")
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div[3]/div/div/div/button"}
(Session info: chrome=93.0.4577.82)```
【问题讨论】:
-
请张贴html。
-
检查元素是
iframe还是shadow-root。 -
@normon99 : 是否可以分享页面网址?
-
我可以,但您需要安装元掩码扩展才能访问该 URL。您会将链接链接到扩展程序吗?我相信它会在您第一次安装时将您重定向到该网址
-
pastebin.com/PnUJtAuY 我试图点击的按钮 // html 是
</style></div><div class="main-container-wrapper"><div class="first-time-flow">
标签: python selenium xpath metamask