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

在Google Colab使用Selenium遇Service无process属性错误求助

问题

在Colab中用Selenium做网页抓取时,执行代码driver=webdriver.Chrome(PATH)触发属性错误:Service object has no attribute process。已经把chromedriver.exe传到sample_data文件夹并复制了路径,但问题没解决。

原代码如下:

from selenium import webdriver 
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
#from webdriver_manager.chrome import ChromeDriverManager
PATH = "/content/sample_data/chromedriver.exe"
#service = Service(r'/content/sample_data/chromedriver.exe')
#driver = webdriver.Chrome(service=Service(PATH))
driver = webdriver.Chrome(PATH)
#driver = webdriver.Chrome(service)
driver.get("https://login.virtualracingschool.com/?email_error=&form_error=&password_error=")
解决方案

核心问题

  1. Colab是Linux环境,Windows版本的chromedriver.exe根本没法在Linux里运行,这是关键问题。
  2. Selenium 4.x版本已经不支持直接把驱动路径传给webdriver.Chrome(),必须通过Service类来管理驱动服务。
  3. Colab默认没装Chrome浏览器,直接跑驱动会报错。

修复步骤

1. 先安装Chrome浏览器

在代码最开头加这两行命令:

!apt-get update
!apt-get install -y chromium-browser

2. 安装匹配的ChromeDriver

推荐直接用命令安装,自动匹配Chrome版本,省得手动找:

!apt-get install -y chromium-chromedriver

安装完可以用!which chromedriver查路径,一般是/usr/lib/chromium-browser/chromedriver。如果非要自己上传,得传Linux版本的ChromeDriver,而且路径要去掉.exe后缀,比如/content/sample_data/chromedriver。

3. 修改Selenium代码

按Selenium 4.x的规范写,还要开无界面模式(Colab必须开,不然跑不起来):

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

# 配置Chrome选项,适配Colab环境
chrome_options = Options()
chrome_options.add_argument('--headless')  # 无界面运行
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# 驱动路径(用命令安装的话直接用这个)
driver_path = '/usr/lib/chromium-browser/chromedriver'
# 自己上传的话用:driver_path = "/content/sample_data/chromedriver"

# 初始化驱动
service = Service(driver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("https://login.virtualracingschool.com/?email_error=&form_error=&password_error=")

4. 更省心的方式:用webdriver-manager自动管理驱动

把注释掉的webdriver-manager用上,它会自动下载匹配版本的驱动,不用手动管路径和版本:
先安装:

!pip install webdriver-manager

然后代码改成:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
driver.get("https://login.virtualracingschool.com/?email_error=&form_error=&password_error=")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 20:10:26