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

Google Colab中Selenium WebDriver初始化报错问题

解决Google Colab中Selenium初始化Chrome驱动的TypeError问题

问题场景

在Google Colab中执行以下Shell脚本完成Chromium、chromium-driver及Selenium的安装后:

%%shell
# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

# Install chromium and chromium-driver
apt-get update
apt-get install chromium chromium-driver

# Install selenium
pip install selenium

运行以下Python代码时出现错误:

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


chrome_options = webdriver.ChromeOptions()
service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')


driver = webdriver.Chrome('chromedriver', options=chrome_options)
driver.get("url")

错误信息

TypeError                                 Traceback (most recent call last)
 in <cell line: 14>()
12
13
---> 14 driver = webdriver.Chrome('chromedriver', options=chrome_options)
15 driver.get("url")
16

TypeError: WebDriver.__init__() got multiple values for argument 'options'

错误原因

  1. Selenium版本参数变化:Selenium 4及以上版本中,webdriver.Chrome()的第一个参数是service对象,而非直接传递驱动路径字符串。直接传'chromedriver'会被识别为service参数,后续又显式传options=chrome_options,导致参数冲突。
  2. 重复定义ChromeOptions:代码中创建了两个ChromeOptions对象chrome_options和options,但最终使用的是未配置任何参数的chrome_options,同时浪费了已配置的options对象。

解决方案

修正代码,正确使用Service对象传递驱动路径,合并ChromeOptions配置:

修正后的Python代码

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

# 仅创建一个ChromeOptions对象并配置必要参数
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# 使用Service对象指定驱动路径
service = Service(executable_path='/usr/bin/chromedriver')

# 初始化Chrome驱动,传递service和options参数
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("url")

关键修正点

  • 只保留一个ChromeOptions对象,将所有启动参数添加到该对象中
  • 通过Service类封装驱动路径,显式传递给webdriver.Chrome()的service参数
  • 确保options参数传递的是已配置好的ChromeOptions对象

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 16:15:56