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)

相关推荐
-To be number.wan7 分钟前
PyCharm接入DeepSeek全教程|3种方法+避坑指南
python·学习·pycharm
Ares-Wang13 分钟前
Python》》FastAPI 异步框架 接口 pymysql【同步】 aiomysql【异步】
开发语言·python·fastapi
SPC的存折21 分钟前
3、Ansible之playbook模块大全
linux·运维·网络·python
雨师@39 分钟前
python包uv使用介绍
开发语言·python·uv
aloha_7891 小时前
软考高项-第二章-信息技术发展
java·人工智能·python·学习
Dxy12393102161 小时前
Python如何删除文件到回收站
开发语言·python
AI-Ming1 小时前
程序员转行学习 AI 大模型: 踩坑记录,HuggingFace镜像设置未生效
人工智能·pytorch·python·gpt·深度学习·学习·agi
阿_旭1 小时前
基于YOLO26深度学习的【电力巡检异常检测与语音提示系统】【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·电力巡检异常检测
zero15971 小时前
Python 8天极速入门笔记(大模型工程师专用):开篇-学习大纲和学习准备
python·学习笔记·大模型编程语言
剑穗挂着新流苏3121 小时前
205_深度学习的非线性魔法:多层感知机(MLP)与激活函数全解析
人工智能·pytorch·python·深度学习