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/

相关推荐
秃了也弱了。14 小时前
WireShark:非常好用的网络抓包工具
网络·测试工具·wireshark
敲上瘾16 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
gc_229921 小时前
使用HtmlAgilityPack+PuppeteerSharp+iText7抓取Selenium帮助文档
selenium·itext7·htmlagilitypack·puppeteersharp
天才测试猿21 小时前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
测试开发Kevin2 天前
详解grafana k6 中stage的核心概念与作用
测试工具·压力测试·grafana
天才测试猿2 天前
接口自动化测试难点:数据库验证解决方案
自动化测试·软件测试·数据库·python·测试工具·职场和发展·接口测试
代码小念3 天前
Pytest+selenium UI自动化测试实战实例(超详细)
selenium·ui·pytest
Mr_Xuhhh3 天前
HTTPS 协议原理
网络·网络协议·测试工具·http·https
测试开发Kevin4 天前
详解k6中的核心概念——场景(Scenarios)
测试工具·压力测试