我正在尝试让 Selenium 导入元掩码。但是,当我使用 XPath 表达式时/html/body/div[1]/div/div[3]/div/div/div/button
,我的控制台返回:
selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“/html/body/div1 https://developer.chrome.com/docs/devtools//div/div[3]/div/div/div/button"}
(会话信息:chrome=93.0.4577.82)
Which is strange as when I use $x("/html/body/div[1]/div/div[3]/div/div/div/button")
in Chrome DevTools https://developer.chrome.com/docs/devtools/, it is able to identify the Get Started button. How can I fix this error and why am I getting said error?
完整源代码:
from selenium import webdriver
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()
完整错误日志:
Traceback (most recent call last):
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)