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

GitLab CI中运行Selenium Python测试遇chromedriver异常退出(状态码127)

GitLab CI中Selenium ChromeDriver执行报错(状态码127)解决方案

问题描述

本地可正常运行Selenium Python测试,但GitLab CI执行时失败。此前CI使用Chrome与chromedriver 114版本可正常运行,因下载链接失效升级至125.0.6422.141后,出现如下错误:

CI配置脚本

erp_test:
  stage: test_Python_3.12
  image: python:3.12
  variables:
    OPERATING_SYSTEM: 'Ubuntu'
  script:
    - python -V
    - pip install -r requirements.txt
    - pip install --upgrade webdriver-manager
  # install google chrome
    - wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    - sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
    - apt-get update
    - echo $(pwd)
    - mkdir -p /opt/google/
    - cd /opt/google/
    - echo $(pwd)
    - wget -q -O ./chrome-linux64.zip https://storage.googleapis.com/chrome-for-testing-public/125.0.6422.141/linux64/chrome-linux64.zip
    - unzip chrome-linux64.zip
    - rm -f chrome-linux64.zip
    - echo $(ls)
    - echo $(ls -ll /opt/google/chrome-linux64/)
    - echo $(ls -ll /usr/bin/)
    - ln -sf /opt/google/chrome-linux64/chrome /usr/bin/google-chrome
    - echo $(ls -ll /usr/bin/)
    - echo $(whereis google-chrome)
    - pytest tests/test_infor_EAM.py

报错日志

@pytest.fixture()
    def setup():
        global driver
        if BROWSER == 'Chrome_Headless':
            chrome_options = Options()
            chrome_options.add_argument('--headless=new')
            chrome_options.add_argument('--window-size=1920,1200')
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_linux_64 = Service('./Drivers/chromedriver-linux64')
>           driver = webdriver.Chrome(service=chrome_linux_64, options=chrome_options)
tests/test_infor_EAM.py:21: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.12/site-packages/selenium/webdriver/chrome/webdriver.py:45: in __init__
    super().__init__(
/usr/local/lib/python3.12/site-packages/selenium/webdriver/chromium/webdriver.py:55: in __init__
    self.service.start()
/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py:102: in start
    self.assert_process_still_running()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <selenium.webdriver.chrome.service.Service object at 0x7fbcef2249e0>
    def assert_process_still_running(self) -> None:
        """Check if the underlying process is still running."""
        return_code = self.process.poll()
        if return_code:
>           raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
E           selenium.common.exceptions.WebDriverException: Message: Service ./Drivers/chromedriver-linux64 unexpectedly exited. Status code was: 127
/usr/local/lib/python3.12/site-packages/selenium/webdriver/common/service.py:115: WebDriverException
=========================== short test summary info ============================
ERROR tests/test_infor_EAM.py::test_login_infor_eam - selenium.common.exceptions.WebDriverException: Message: Service ./Drivers/chromedriver-linux64 unexpectedly exited. Status code was: 127
=============================== 1 error in 0.26s ===============================

解决方案

状态码127通常表示文件无执行权限、依赖缺失或路径错误,按以下步骤排查修复:

1. 为chromedriver添加执行权限

在CI脚本的pytest命令前,添加权限赋予命令:

chmod +x ./Drivers/chromedriver-linux64

2. 安装ChromeDriver依赖库

Debian/Ubuntu镜像中缺少chromedriver运行所需的系统依赖,在安装Chrome后添加:

apt-get install -y libnss3 libgconf-2-4 libxss1 libappindicator1 libindicator7

3. 改用WebDriverManager自动管理Driver(推荐)

既然已安装webdriver-manager,直接用它自动匹配Chrome版本并下载Driver,无需手动维护Driver文件:
修改测试代码中的fixture:

from webdriver_manager.chrome import ChromeDriverManager

@pytest.fixture()
def setup():
    global driver
    if BROWSER == 'Chrome_Headless':
        chrome_options = Options()
        chrome_options.add_argument('--headless=new')
        chrome_options.add_argument('--window-size=1920,1200')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        # 自动获取对应版本的chromedriver
        driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

4. 验证Chrome可正常启动

在CI脚本中添加验证命令,确保Chrome安装正确:

google-chrome --version
google-chrome --headless --no-sandbox --disable-dev-shm-usage --dump-dom https://www.google.com

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 09:35:18