selenium定位元素报错:‘WebDriver‘ object has no attribute ‘find_element_by_id‘

Selenium更新到 4.x版本后,以前的一些常用的代码的语法发生了改变

复制代码
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.baidu.com')
input = browser.find_element_by_id(By.ID,'kw')
input.send_keys('Python')

目标:希望通过 selenium 模拟在 "百度" 上 输入关键词搜索

思路:通过对网页的源代码分析(进入www.baidu.com,右键并检查则可看其HTML源代码),定位到搜索框的属性id="kw"

报错:

复制代码
AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

报错原因:

selenium 更新到 4.x 之后,很多api发生了变化

解决方案:

复制代码
#input = browser.find_element_by_id('kw')
# 替换为
input = browser.find_element(By.ID,'kw')

当然最最推荐的还是去官方查看文档:https://www.selenium.dev/documentation/webdriver/getting_started/first_script/

参考资料:

https://blog.csdn.net/m0_49076971/article/details/126233151

https://www.selenium.dev/documentation/webdriver/getting_started/first_script/

相关推荐
测试老哥10 小时前
软件测试:单元测试详解
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
自动化测试行业观察2 天前
从“自动化”到“智能化”:TestMan AI测试平台引领软件测试范式转移
运维·自动化测试·人工智能·测试工具·自动化·app测试·移动应用测试
Hy行者勇哥2 天前
Coze 搭建【软件测试助手 Skill】可行分析及操作实践
测试工具·压力测试
我不满意2 天前
loadrunner-11安装+破解+汉化(提供安装包,破解方式,汉化包)
测试工具·microsoft
lemon_linaa2 天前
PentAGI 自动化渗透测试工具试用
测试工具·开源·自动化·安全性测试
goodesocket2 天前
市场国产替代的国产存储芯片测试座厂家芯片检测利器
测试工具
天才测试猿3 天前
2026软件测试面试八股文(含答案+文档)
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
不可能掉发3 天前
Env Guard:让浏览器一眼分清生产、测试和开发环境
前端·javascript·chrome·测试工具·html·开源软件·个人开发