find_element_by_id()方法的使用上。这个方法已经被弃用,建议使用find_element()方法替代。

python 复制代码
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url)
button = browser.find_element_by_id('su')
print(button)

修改后代码

python 复制代码
from selenium import webdriver
path = 'chromedriver.exe'
browser = webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url)
# 使用 find_element() 方法替代 find_element_by_id()
button = browser.find_element('id', 'su')
print(button)

报错原因:webdriver.Chrome()的参数路径错误。

修改后的代码:

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

path = 'chromedriver.exe'
service = Service(executable_path=path)
browser = webdriver.Chrome(service=service)

url = 'https://www.baidu.com'

browser.get(url)

# 使用 find_element() 方法替代 find_element_by_id()
button = browser.find_element('id', 'su')
print(button)

但是,虽然出现报错信息,但是有运行结果,可以修改代码,也可以不修改!!!

相关推荐
杜子不疼.4 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
有点。4 小时前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
时间的拾荒人4 小时前
C语言编译与链接:从源码到可执行程序的完整解析
c语言·开发语言
人道领域4 小时前
【0-1的agent进阶篇】Prompt 与上下文工程
java·开发语言·prompt·mcp
aaPIXa6224 小时前
C++大型项目模块化拆分实战记录
开发语言·c++
AOwhisky4 小时前
Python 基础语法(上篇 + 下篇)——综合自测题
linux·windows·python
z落落4 小时前
C# WinForm 线程池与事件等待+进度条暂停恢复实战案例
开发语言·c#
石山代码4 小时前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23
浮江雾4 小时前
Flutter第四节------核心概念学习笔记:从基础语法到异步编程
linux·服务器·开发语言·笔记·flutter·入门·基础
北冥you鱼5 小时前
Go Modules 使用指南:从入门到精通
开发语言·后端·golang