selenium解决调用Chrome str’ object has no attribute ‘capabilities’ Process finished

使用selenium调用chrome进行自动化测试,时不时报错:

原来代码如下:

复制代码
path_to_chromedriver = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
5driver = webdriver.Chrome(executable_path=path_to_chromedriver)

报错如下:

上面的代码修改后报错:Traceback (most recent call last): File "D:\program files\Python\Python39\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path path = SeleniumManager().driver_location(options) if path is None else path File "D:\program files\Python\Python39\lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 76, in driver_location browser = options.capabilities["browserName"] AttributeError: 'str' object has no attribute 'capabilities' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\newcode\pythonProject\gpt\doubao_csdn.py", line 130, in <module> reader = FunnyScriptsReader(directory) File "D:\newcode\pythonProject\gpt\doubao_csdn.py", line 21, in init self.driver = webdriver.Chrome("executable_path=C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe") File "D:\program files\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in init super().init( File "D:\program files\Python\Python39\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in init self.service.path = DriverFinder.get_path(self.service, options) File "D:\program files\Python\Python39\lib\site-packages\selenium\webdriver\common\driver_finder.py", line 40, in get_path msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager." AttributeError: 'str' object has no attribute 'capabilities' Process finished with exit code 1

在最新版本的Selenium中,推荐的做法是使用 service 模块,并且不再推荐使用 executable_path。因此,更好的实践是创建一个 Service 实例,,并传入 ChromeDriver 的路径:(

顺利解决了上面的问题 )

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

替换为实际的ChromeDriver路径

path_to_chromedriver = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'

service = Service(executable_path=path_to_chromedriver)

创建WebDriver实例

driver = webdriver.Chrome(service=service)

相关推荐
喵手3 小时前
Python爬虫实战:Boss直聘职位数据采集实战 - Playwright + 结构化解析完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·sqlite·爬虫实战·playwright·boss直聘职位数据采集·结构化解析
夜瞬3 小时前
【Flask 框架学习】02:核心基本概念全解析
python·flask·web
ding_zhikai3 小时前
【Web应用开发笔记】Django笔记2:一个 Hello World 网页
笔记·后端·python·django
kyle~3 小时前
Python---webbrowser库 跨平台打开浏览器的控制接口
开发语言·python·web
一尘之中3 小时前
量子力学数学基础入门:从态矢到内积外积(附Python演示)
python·ai写作·量子计算
七夜zippoe3 小时前
性能测试实战:Locust负载测试框架深度指南
分布式·python·性能测试·locust·性能基准
有点心急10213 小时前
SQL 执行 MCP 工具开发(一)
人工智能·python·aigc
belldeep3 小时前
python:Flask 3, mistune 2, 实现在线编辑 Markdown 文档的 Web 服务程序
python·flask·markdown·mistune·pygments
apcipot_rain3 小时前
python 多进程多线程 学习笔记
笔记·python·学习
紫微AI6 小时前
WebMCP:开启 Agentic Web 新时代——Chrome 新 API 的特性与前瞻
前端·chrome