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

使用Selenium的webdriver时出现AttributeError错误求助

解决Selenium 'str' object has no attribute 'capabilities' 错误

这个错误是因为Selenium 4.x及以上版本的API变更,不再支持直接将ChromeDriver的路径字符串传递给webdriver.Chrome()构造函数。需要通过Service类来管理驱动路径,具体修改如下:

修改后的代码

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

PATH = "C:\\Users\\winwin\\Documents\\Visual Studio\\chromedriver.exe"
URL = "https://scratch.mit.edu/projects/994662672/"

# 创建Service实例传入驱动路径
service = Service(PATH)
# 将Service对象传给Chrome构造函数
driver = webdriver.Chrome(service=service)
driver.get(URL)

额外优化方案(可选)

如果不想手动管理ChromeDriver路径,可以使用webdriver-manager库自动匹配并下载对应版本的驱动:

  1. 先安装库:
pip install webdriver-manager
  1. 使用代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

URL = "https://scratch.mit.edu/projects/994662672/"

# 自动获取并使用对应版本的ChromeDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(URL)

注意事项

  • 确保Chrome浏览器和ChromeDriver的版本完全匹配(大版本一致即可,比如Chrome 118对应ChromeDriver 118.x)
  • 如果之前配置过环境变量,也可以不指定路径,Selenium会自动查找系统中的ChromeDriver

内容的提问来源于stack exchange,提问作者Larcury

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 04:59:52