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/

相关推荐
半路_出家ren9 小时前
流量抓取工具(wireshark)
网络·网络协议·测试工具·网络安全·wireshark·流量抓取工具
猿周LV10 小时前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
西柚小萌新1 天前
【Python爬虫基础篇】--4.Selenium入门详细教程
爬虫·python·selenium
??? Meggie1 天前
Selenium 怎么加入代理IP,以及怎么检测爬虫运行的时候,是否用了代理IP?
爬虫·tcp/ip·selenium
天才测试猿1 天前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
HtwHUAT1 天前
五、web自动化测试01
前端·css·chrome·python·功能测试·selenium·html
远方2351 天前
应用信息1.13.0发布
测试工具·安全·apk·开发工具·应用·工具·信息
珠峰下的沙砾2 天前
如何在 Postman 中,自动获取 Token 并将其赋值到环境变量
测试工具·lua·postman
桑榆非婉2 天前
同样的接口用postman/apifox能跑通,用jmeter跑就报错500
测试工具·jmeter·postman
苍煜2 天前
Jsoup、Selenium 和 Playwright 的含义、作用和区别
python·selenium·测试工具