如何修复 NoSuchElementException(将 XPath 表达式与 Selenium 结合使用)

2024-01-04

我正在尝试让 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)

Selenium 中有四种点击方式。

我将使用这个 XPath 表达式:

//button[text()='Get Started']

代码试用1:

time.sleep(5)
driver.find_element_by_xpath("//button[text()='Get Started']").click()

代码试用2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Get Started']"))).click()

代码试用3:

time.sleep(5)
button = driver.find_element_by_xpath("//button[text()='Get Started']")
driver.execute_script("arguments[0].click();", button)

代码试用4:

time.sleep(5)
button = driver.find_element_by_xpath("//button[text()='Get Started']")
ActionChains(driver).move_to_element(button).click().perform()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS:请检查开发工具中是否有 HTML 中的唯一条目DOM https://en.wikipedia.org/wiki/Document_Object_Model or not.

检查步骤:

Press F12 in Chrome → go to element section → do a Ctrl + F → then paste the XPath expression and see, if your desired element is getting highlighted.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复 NoSuchElementException(将 XPath 表达式与 Selenium 结合使用) 的相关文章

随机推荐

  • 使用 fortran 覆盖文件

    我正在使用 Fortran 90 程序来写入文件 该文件的第一行应该指示剩余文件中的行数 当满足某个标准且无法事先确定时 程序将写入该文件 基本上 只有在运行结束后我才会知道总行数 我想按以下方式进行 1 打开文件并在第一行写入一些文本 例
  • 计算代码的失败次数!

    这真的很花我的时间 我找不到一种简单的方法来估计以下代码 循环 的 FLOPS 循环的单次迭代有多少 FLOPS float func float atominfo float energygridItem int xindex int y
  • 如何在java中将camelCase字符串转换为下划线,保留一些大写,其余的小写?

    如何在java中将驼峰式字符串转换为下划线并保留一些大写字母 我正在使用这段代码 String inputString Hi How areYouToday String result inputString replaceAll a z
  • nullglob 禁用路径名制表符补全

    我发现shopt s nullglob显然禁用了文件和目录的制表符补全 并且shopt u nullglob恢复它 为什么目录的制表符补全显然依赖于nullglob未设置 我正在使用 Bash4 2 37 1 release在 Debian
  • 使用 compojure 从数据库提供二进制文件

    我有以下路线定义 require compojure core as ccore ring util response as response def main routes ccore defroutes avalanche routes
  • 无法创建持久存储协调器

    我收到以下错误 Terminating app due to uncaught exception NSInternalInconsistencyExprojectception reason Managed object store fa
  • 每 X 秒从 ArrayList 更新一次 JLabel - Java

    我有一个简单的Java程序 它读取一个文本文件 用 空格 分割它 显示第一个单词 等待2秒 显示下一个 等等 我想在Spring中执行此操作或其他一些 GUI 关于如何使用 spring 轻松更新单词有什么建议吗 遍历我的列表并以某种方式使
  • C# 中 AVL 树的性能

    我用C 实现了一个AVL树 其插入矩阵如下 Number of Elements Time taken to insert sec 10 0 067 100 0 073 200 0 112 500 0 388 900 1 205 1000
  • 如何通过 Concourse CI 管道发送电子邮件?

    如何设置在 Concourse 中作业完成或失败时发送的电子邮件 您可以使用https github com pivotal cf email resource https github com pivotal cf email resou
  • iPad 版 iOS 8.3 上 UIAlertView 关闭后键盘会弹出

    随着最新的 iOS 8 3 版本的发布 我们的应用程序开始出现奇怪的行为 完成文本字段编辑后 用户可以单击关闭按钮 弹出一个UIAlertView 当用户点击时discard在警报视图中 警报视图和当前视图被忽略 但不知怎的 键盘在视图消失
  • 从静态方法调用 finish()

    我正在使用 Facebook Android SDK 并希望在用户登录并获取用户对象后关闭我的 Activity 在实践中 我存储了其中的一部分 但无论如何我都想关闭该活动 make request to the me API Reques
  • Asp.Net Core 2.0 中的“HttpPostedFileBase”

    我最近正在开发一个调用 API 的 ReactJS 应用程序 使用 NET Core 2 0 开发 我的问题是如何使用HttpPostedFileBase在 NET Core 2 0 API 中 以获取文件内容并将其保存在数据库中 你没有H
  • python实时写入文件

    我有这段 python 代码 它循环遍历文本文件 urls txt 中的 url 列表 然后跟踪所有 url 的重定向 如果 url 包含特定字符串 则会将其写入名为 redirects txt 的文件中 import urllib req
  • 生产中头变量丢失

    我正在运行 Rails 3 1 后面有 PhusionPassenger 和 NGINX 我通过一个简单的 HttpClient OS X 的 GrahpicalHttpClient 发送请求 我的代码需要标头中的令牌和 ID 来验证调用者
  • 在Prepare()方法中找不到高级通配符映射参数

    从文档中 Struts2 的高级通配符映射 http struts apache org release 2 3 x docs wildcard mappings html 高级通配符 从2 1 9 开始可以在action中定义正则表达式
  • Visual Studio Ultimate 2013 安装失败

    我有一台 CLEVO x7200 Intel i7 970 3 20GHz 12Gb RAM 2x512Gb HDD 2xATI Radeon HD6970M 2Gb Crossfire 我有一个非常恼人的问题 我的笔记本电脑运行 Wind
  • 项目文件受版本控制吗?

    我从事一个大型项目 其中除项目文件之外的所有源文件都存储在版本控制中 这是首席开发人员的决定 他的理由是 协调开发人员工作目录之间的差异非常耗时 它允许开发人员独立工作 直到他们的更改稳定为止 相反 开发人员最初会获得其他开发人员的项目文件
  • os.path.dirname(os.path.abspath(__file__)) 和 os.path.dirname(__file__) 之间的区别

    settings pyDjango 项目的文件包含这两行 BASE DIR os path dirname os path dirname os path abspath file PROJECT ROOT os path dirname
  • 检查传递给泛型方法的类型

    如何检查作为泛型方法的类型参数传递的类型是什么 foo
  • 如何修复 NoSuchElementException(将 XPath 表达式与 Selenium 结合使用)

    我正在尝试让 Selenium 导入元掩码 但是 当我使用 XPath 表达式时 html body div 1 div div 3 div div div button 我的控制台返回 selenium common exceptions