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)

相关推荐
哈茶真的c8 分钟前
【书籍心得】左耳听风:传奇程序员练级攻略
java·c语言·python·go
io_T_T1 小时前
Paddle-CLS图像分类_环境安装
python·日常软硬件经验分享
百***48071 小时前
Python使用PyMySQL操作MySQL完整指南
数据库·python·mysql
PNP Robotics2 小时前
PNP机器人上海宝山智能机器人年会发表机器人10年主题演讲演讲
人工智能·python·机器人
___波子 Pro Max.2 小时前
Python获取当前脚本目录路径
python
努力成为大牛吧2 小时前
Pycharm 接入 Deepseek API完整版教程
ide·python·pycharm
闲人编程2 小时前
Python对象模型:一切都是对象的设计哲学
开发语言·python·模型·对象·codecapsule·下划线
二川bro2 小时前
Python大语言模型调优:LLM微调完整实践指南
开发语言·python·语言模型
wa的一声哭了2 小时前
Webase部署Webase-Web在合约IDE页面一直转圈
linux·运维·服务器·前端·python·区块链·ssh
坚持就完事了3 小时前
蓝桥杯中Python常用的库与模块
python·算法