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

在CodeSpace运行Selenium+ChromeDriver遇'Service'无'process'属性错误求助

解决Selenium中Service对象无process属性的AttributeError问题

问题场景

在CodeSpace/Workspace环境运行Selenium测试时,出现如下错误:

Traceback (most recent call last):
  File "/workspaces/117965322/SeleniumTest", line 5, in <module>
    driver = webdriver.Chrome(service=ser)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    super().__init__(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 103, in __init__
    self.service.start()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 106, in start
    self.assert_process_still_running()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 117, in assert_process_still_running
    return_code = self.process.poll()
AttributeError: 'Service' object has no attribute 'process'

当前运行的代码:

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

ser = Service("/home/ubuntu/.local/lib/python3.10/site-packages/chromedriver")
driver = webdriver.Chrome(service=ser)
driver.get("https://www.google.com")
driver.quit()

已导入selenium.webdriver.chrome.service,但仍报错。

解决方案

该错误本质是ChromeDriver服务启动失败,导致process属性未被初始化,可按以下步骤排查修复:

1. 确保Chrome浏览器已安装

在CodeSpace终端执行:

google-chrome --version

若未安装,执行安装命令:

sudo apt update && sudo apt install -y google-chrome-stable

2. 自动管理ChromeDriver版本(推荐)

手动指定路径易出现版本不匹配或路径错误,使用webdriver-manager可自动匹配对应版本的ChromeDriver:

  • 先安装依赖:
pip install webdriver-manager
  • 修改代码为:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
driver.quit()

3. 添加无GUI运行参数

CodeSpace属于无图形界面环境,需配置Chrome无头模式:

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

# 配置无头模式及必要参数
options = Options()
options.add_argument("--headless=new")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")
print(driver.title)  # 验证是否成功访问
driver.quit()

4. 更新Selenium到最新版本

旧版本Selenium可能存在兼容性问题:

pip install --upgrade selenium

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 07:15:55