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/

相关推荐
wdxylb11 小时前
Pyhton爬虫使用Selenium实现浏览器自动化操作抓取网页
爬虫·selenium·测试工具
码力码力我爱你1 天前
C++ WebDriver扩展
c++·selenium·测试工具
能摆一天是一天2 天前
Python 爬虫 根据ID获得UP视频信息
开发语言·爬虫·python·selenium
土小帽软件测试2 天前
fiddler抓包16_自动重定向(mock数据)
测试工具·fiddler·抓包·软件测试学习
Kakaxiii2 天前
UMLS的api调用(Postman)
人工智能·深度学习·测试工具·自然语言处理·postman
可遇_不可求3 天前
使用Charles进行Mock数据:高效提升前端交互测试效率
测试工具
NPE~3 天前
爬虫入门 & Selenium使用
爬虫·python·selenium·测试工具·xpath
sp_fyf_20243 天前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-09-29
人工智能·深度学习·神经网络·测试工具·算法·语言模型·软件工程
吾爱星辰4 天前
【Kotlin基于selenium实现自动化测试】初识selenium以及搭建项目基本骨架(1)
java·开发语言·jvm·selenium·kotlin
小旺不正经5 天前
selenium过webdriver检测
python·selenium·测试工具