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)

相关推荐
AI量化投资实验室17 分钟前
年化398%,回撤11%,夏普比5,免费订阅,5积分可查看参数|多智能体的架构设计|akshare的期货MCP代码
人工智能·python
夫唯不争,故无尤也1 小时前
AI调度框架全解析:从通用到LLM专用
python·大模型·调用框架
wudl55661 小时前
Python 虚拟环境和包管理
数据库·python·sqlite
Geoking.3 小时前
PyTorch torch.unique() 基础与实战
人工智能·pytorch·python
俊俊谢3 小时前
【第一章】金融数据的获取——金融量化学习入门笔记
笔记·python·学习·金融·量化·akshare
xiecoding.cn3 小时前
Selenium IDE下载和安装教程(附安装包)
selenium·测试工具·selenium安装·selenium ide·selenium ide下载·selenium下载
闲人编程4 小时前
现代Python开发环境搭建(VSCode + Dev Containers)
开发语言·vscode·python·容器·dev·codecapsule
nvd115 小时前
python异步编程 -- 深入理解事件循环event-loop
python
chenchihwen5 小时前
AI代码开发宝库系列:Text2SQL深度解析基于LangChain构建
人工智能·python·langchain·text2sql·rag
CILMY236 小时前
【一问专栏】Python中is和==的区别详解
开发语言·python·is·==