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)

相关推荐
nenchoumi311913 分钟前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
GoodStudyAndDayDayUp14 分钟前
初入 python Django 框架总结
数据库·python·django
星辰大海的精灵22 分钟前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
精灵vector27 分钟前
Agent短期记忆的几种持久化存储方式
人工智能·python
北京_宏哥42 分钟前
🔥Python零基础从入门到精通详细教程4-数据类型的转换- 上篇
前端·python·面试
乾巫宇宙国监察特使1 小时前
Python的设计模式
python·测试
Hockor1 小时前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
这里有鱼汤1 小时前
熟练掌握MACD这8种形态,让你少走三年弯路(附Python量化代码)| 建议收藏
后端·python
404.Not Found1 小时前
Day46 Python打卡训练营
开发语言·python
love530love1 小时前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm