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)

相关推荐
3824278272 小时前
python3网络爬虫开发实战 第二版:绑定回调
开发语言·数据库·python
dagouaofei2 小时前
培训项目总结 PPT 工具对比评测,哪款更专业
python·powerpoint
Hello eveybody2 小时前
用代码生成你的电影预告片(Python)
python
山沐与山2 小时前
【设计模式】 Python代理模式:从入门到实战
python·设计模式·代理模式
言之。3 小时前
Python调用DeepSeek API查询ClickHouse
windows·python·clickhouse
SCBAiotAigc3 小时前
Chrome的cookie编辑插件EditThisCookie
人工智能·chrome·python·ubuntu
dagouaofei3 小时前
运营述职 PPT 工具横评:效率与呈现谁更优
python·powerpoint
先做个垃圾出来………3 小时前
Python测试桩工具
java·开发语言·python
小芳矶3 小时前
【langchain框架——检索链】利用检索链创建自己的购物知识库并完成智能体的商品推荐
java·python·langchain
就叫飞六吧3 小时前
py脚本一键生成常见文件格式案例
开发语言·python