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

使用Python Selenium调用ChromeDriver打开网站失败求助

解决ChromeDriver启动报错'str' object has no attribute 'capabilities'的方案

错误原因

Selenium 4.x版本开始,webdriver.Chrome()的初始化参数不再接受直接传入驱动路径字符串,必须通过ChromeService类来配置驱动路径,这是导致报错的核心原因。

修正步骤及代码示例

  1. 安装/更新Selenium
    先执行正确的安装命令(你之前写的!pipe是笔误,应为!pip):

    !pip install selenium
    
  2. 使用ChromeService指定驱动路径
    导入ChromeService类封装chromedriver路径,再传入webdriver.Chrome()。同时注意Windows路径的转义(用双反斜杠\\或原始字符串r""),且driver.get()必须传入带协议的完整URL。

针对驱动存放在C:\Program Files (x86)的情况:

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

# 用原始字符串避免转义问题,或把单反斜杠改为双反斜杠
PATH = r"C:\Program Files (x86)\chromedriver.exe"
service = Service(PATH)
driver = webdriver.Chrome(service=service)
# 传入完整的带协议URL
driver.get("https://www.google.com")

针对驱动和Notebook同目录的相对路径情况:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys

service = Service("./chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.google.com")
  1. 额外检查点
    • 你的Chrome版本(116.0.5845.141)和ChromeDriver版本(116.0.5845.96)属于同一大版本,兼容性没问题。
    • Windows系统下确保对驱动所在目录有读写权限,若权限不足可将驱动移至Notebook同目录等更易访问的路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 18:33:20