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

Selenium库使用异常:初始化ChromeDriver报AttributeError错误

问题:Selenium代码运行报错 'PosixPath' object has no attribute 'capabilities'

原代码

from selenium import webdriver
from pathlib import Path

chromedriver_path:str = Path(__file__).absolute().parent / 'chromedriver'
driver = webdriver.Chrome(chromedriver_path)
driver.get("www.google.com")

报错信息

Traceback (most recent call last):
  File "/Users/macklee/anaconda3/lib/python3.11/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 "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/common/selenium_manager.py", line 87, in driver_location
    browser = options.capabilities["browserName"]
              ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'capabilities'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/macklee/Desktop/Computer science/Studies/Python/Paulla gomes project/navagation.py", line 5, in 
    driver = webdriver.Chrome(chromedriver_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/Users/macklee/anaconda3/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 49, in __init__
    self.service.path = DriverFinder.get_path(self.service, options)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/macklee/anaconda3/lib/python3.11/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: 'PosixPath' object has no attribute 'capabilities'

错误原因

Selenium 4.x版本重构了API,webdriver.Chrome()的第一个参数不再是驱动路径,而是options对象。你直接传入PosixPath类型的驱动路径,程序会把它误认为是options参数,导致尝试访问其capabilities属性时失败。

另外,driver.get("www.google.com")缺少协议头(http/https),运行时会抛出无效URL的错误,需要一并修正。

修复方案

方案1:手动指定驱动路径(使用Service类)

导入ChromeService,将驱动路径传入Service实例,再把Service传给Chrome的service参数:

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

chromedriver_path = Path(__file__).absolute().parent / 'chromedriver'
# 创建Service实例
service = Service(chromedriver_path)
# 初始化Chrome时传入service参数
driver = webdriver.Chrome(service=service)
# 添加协议头
driver.get("https://www.google.com")

方案2:使用Selenium Manager自动管理驱动(推荐)

Selenium 4.6+内置Selenium Manager,无需手动下载和指定驱动路径,直接初始化即可:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.google.com")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 04:18:20