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/

相关推荐
程序员三藏4 小时前
Jmeter自动化测试
自动化测试·软件测试·python·测试工具·jmeter·测试用例·接口测试
西游音月9 小时前
(4)pytest+Selenium自动化测试-元素定位之CSS Selector定位
css·selenium·pytest
青青子衿_2119 小时前
TikTok爬取——视频、元数据、一级评论
爬虫·python·selenium
程序员小远2 天前
Appium-移动端自动测试框架详解
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
johnny2332 天前
AI加持测试工具汇总:Strix、
人工智能·测试工具
韩师学子--小倪2 天前
tcpdump + Wireshark:抓取nginx转发到服务的流量
测试工具·nginx·tcpdump
百花~2 天前
自动化测试概念篇~
selenium·log4j
一晌小贪欢2 天前
【Python办公】用 Selenium 自动化网页批量录入
开发语言·python·selenium·自动化·python3·python学习·网页自动化
代码的乐趣2 天前
支持selenium的chrome driver更新到142.0.7444.175
chrome·python·selenium