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/

相关推荐
Meta Luo1 天前
UI 自动化测试中的等待、超时和异步加载怎么处理
功能测试·测试工具·ui·回归·自动化
Data_Journal2 天前
如何使用 Selenium 和 Node.js 绕过 CAPTCHA
selenium
yeflx2 天前
Ubuntu22.04下通过Wireshark抓取相机IP
tcp/ip·测试工具·wireshark
总线通信小百科2 天前
100BASE-T1媒体转换器选型:接口、主从模式与供电怎么判断?
经验分享·测试工具
郝学胜-神的一滴2 天前
Python浏览器自动化:Selenium与Playwright实战对比,网页爬虫&自动操作一站式指南
开发语言·爬虫·python·selenium·程序人生·自动化
测试开发技术2 天前
AI时代我们要如何玩抓包测试:Reqable + AI Agent
软件测试·人工智能·测试工具·接口测试·agent·抓包
泽众云测试3 天前
国产化替代国外性能测试工具推荐:泽众PerformanceRunner
软件测试·测试工具·性能测试
墨云安全3 天前
自动化渗透测试工具有哪些?2026 年引擎原理、任务编排与误报抑制实践
git·测试工具·网络安全·渗透测试·自动化
芝麻馅汤圆儿3 天前
sfnt-pingpong -测试网络性能和延迟的工具
测试工具
Wokoo73 天前
Selenium 核心 API 解析(三)
python·功能测试·selenium