AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

在Selenium 4及以上版本中,find_element_by_* 和 find_elements_by_* 这类方法已经被弃用并移除了,所以使用会报错:

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

从Selenium 4开始,推荐使用新的方法find_element()和find_elements()结合By类来定位元素。

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

# 1. 按 id 定位
element = driver.find_element(By.ID, "element-id")

# 2. 按 name 定位
element = driver.find_element(By.NAME, "element-name")

# 3. 按 class 定位
element = driver.find_element(By.CLASS_NAME, "element-class")

# 4. 按 tag 标签定位
element = driver.find_element(By.TAG_NAME, "input")

# 5. 按链接文本定位
element = driver.find_element(By.LINK_TEXT, "Click Here")

# 6. 按部分链接文本定位
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Click")

# 7. 按 xpath 定位
element = driver.find_element(By.XPATH, "//div[@id='element-id']")

# 8. 按 css 选择器定位
element = driver.find_element(By.CSS_SELECTOR, "#element-id .element-class")
相关推荐
实名上网宋凯宣8 小时前
Django
后端·python·django
如若1239 小时前
【论文复现】CoFailMed:从零复现医学图像分割的共享失败审计,含完整代码流程与科研绘图
人工智能·python
砚底藏山河10 小时前
python量化自定义周期 K 线合成
java·开发语言·数据库·python·金融
视觉小萌新11 小时前
AI Class 3 · Chapter 1:变量、字符串、list/dict、if/for(python学习-小白版-有经验可跳过)
python·学习·list
薛定谔的猫-菜鸟程序员11 小时前
软件架构设计师开发资料大全(从入门到精通)
后端·python·系统架构·flask·架构设计师
Ramble_Naylor13 小时前
Rust深度解读:Send 与 Sync 的本质与线程安全边界
python·算法·rust
Allen_LVyingbo14 小时前
NVIDIA CUDA Tile 在医疗计算中的算子形式化、算法重构与内核实现
大数据·python·算法·重构·知识图谱·健康医疗·数据库架构
云和数据.ChenGuang14 小时前
Python 闭包详解:从原理到实战
linux·开发语言·人工智能·python·kubernetes
我命由我1234515 小时前
Python 实用操作 - 文件操作(打开与关闭文件、读取文件、文件写入)
java·开发语言·前端·javascript·数据库·python·java-ee
青少儿编程课堂15 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:倍增法求最近公共祖先LCA解析
c++·python·算法·bfs·信息学竞赛