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

在Colab中使用Selenium报错:'str'对象没有'capabilities'属性

解决Colab中Selenium的AttributeError问题

问题根源

报错AttributeError: 'str' object has no attribute 'capabilities'是因为初始化WebDriver时,错误传入了字符串而非Selenium对应的Options对象,导致Selenium Manager无法读取浏览器配置信息。

解决方案步骤

  • 必须使用对应浏览器的Options类创建配置对象,而非直接传入浏览器名称字符串
  • Colab为无头环境,需添加无头模式及必要的浏览器启动参数

完整可运行示例代码

# 1. 安装必要依赖(首次运行执行)
!pip install selenium
!apt-get update
!apt install chromium-chromedriver

# 2. 导入模块
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 3. 配置Chrome Options
chrome_options = Options()
chrome_options.add_argument('--headless')  # 无头模式适配Colab无界面环境
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# 4. 初始化WebDriver(传入配置好的options对象,而非字符串)
driver = webdriver.Chrome(options=chrome_options)

# 测试访问页面
driver.get("https://www.google.com")
print(driver.title)

# 关闭驱动
driver.quit()

常见错误点排查

  • 禁止直接写webdriver.Chrome("chrome")这类传入字符串的写法,必须传入Options实例
  • 若使用Firefox,对应改用webdriver.FirefoxOptions(),并确保安装geckodriver

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:09:57