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

Selenium与Python配置ChromeDriver路径报错求助

解决Selenium 4.x中ChromeDriver初始化的AttributeError问题

问题描述

使用VS Code进行网页抓取,尝试多种路径版本后仍无法运行以下Selenium代码:

from selenium import webdriver

# Specify the path to the Chrome WebDriver executable
PATH = "C:\\Users\\CHEEMN2\\Downloads\\chromedriver-win64\\chromedriver-win64/to/chromedriver.exe"  # Replace with the actual path on your system

# Create a Chrome WebDriver instance
driver = webdriver.Chrome(executable_path=PATH)

# Open a URL
driver.get("https://www.google.com/")

# Close the WebDriver when done
driver.quit()

运行后持续出现如下错误:

[Running] python -u "c:\\Users\\CHEEMN2\\OneDrive - CPGGPC\\Python\\import os.py"
Traceback (most recent call last):
  File "C:\\Users\\CHEEMN2\\AppData\\Local\\Programs\\Python\\Python311\\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 "C:\\Users\\CHEEMN2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\selenium\\webdriver\\common\\selenium_manager.py", line 71, 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 "c:\\Users\\CHEEMN2\\OneDrive - CPGGPC\\Python\\import os.py", line 6, in <module>
    driver = webdriver.Chrome(driver_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\\Users\\CHEEMN2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\selenium\\webdriver\\chrome\\webdriver.py", line 45, in __init__
    super().__init__(
  File "C:\\Users\\CHEEMN2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\selenium\\webdriver\\chromium\\webdriver.py", line 51, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\\Users\\CHEEMN2\\AppData\\Local\\Programs\\Python\\Python311\\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'

[Done] exited with code=1 in 0.683 seconds

环境信息

  • Python版本:3.11.4
  • Chrome浏览器版本:116.0.5845.97(官方版本,64位)
  • ChromeDriver版本:win64的116.0.5845.96
  • Selenium版本:4.11.2

解决方案

1. 使用Service类指定ChromeDriver路径(推荐)

Selenium 4.x版本开始,webdriver.Chrome()不再直接接受executable_path作为位置参数,需要通过Service类传入驱动路径:

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

# 统一路径分隔符,Windows下用双反斜杠或单斜杠都可
PATH = "C:\\Users\\CHEEMN2\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe"

# 创建Service对象,传入驱动路径
service = Service(PATH)
# 初始化Chrome驱动
driver = webdriver.Chrome(service=service)

driver.get("https://www.google.com/")
driver.quit()

2. 让Selenium Manager自动管理驱动(更简便)

Selenium 4.6+版本自带的Selenium Manager可以自动下载匹配浏览器版本的驱动,无需手动指定路径,代码简化为:

from selenium import webdriver

# 直接初始化,Selenium会自动处理驱动
driver = webdriver.Chrome()

driver.get("https://www.google.com/")
driver.quit()

3. 检查路径格式问题

原代码中路径混合了\\和/,虽然Windows系统兼容,但建议统一使用双反斜杠\\或单斜杠/,避免路径解析错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 02:07:11