You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python+Selenium调用Chrome时出现Attribute Error问题求助

解决Selenium调用Chrome时的AttributeError: 'str' object has no attribute 'capabilities'

这个错误是因为Selenium 4.x及以上版本调整了API,webdriver.Chrome() 不再直接接受驱动文件路径字符串,必须通过 Service 类来传递驱动路径。

修正后的代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

# 用Service类封装ChromeDriver路径
service = Service(ChromeDriverManager().install())
browser = webdriver.Chrome(service=service)
browser.get('https://google.com')

手动指定驱动文件的情况

如果是手动下载了ChromeDriver,也需要用Service封装路径:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

# 替换为你的ChromeDriver实际路径
service = Service('/path/to/your/chromedriver')
browser = webdriver.Chrome(service=service)
browser.get('https://google.com')

内容的提问来源于stack exchange,提问作者abd hadi Sakbani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 05:03:15