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

Docker部署Selenium+pytest自动化测试遇chromedriver异常退出求助

Docker中Selenium+pytest自动化测试报错排查与解决

问题概述

用Python 3.10、Selenium 4.11.2、pytest 7.4.0做自动化测试,部署到Docker容器后运行测试出现两个核心错误:

  • 初始错误:selenium.common.exceptions.WebDriverException: Message: Service /root/.cache/selenium/chromedriver/linux64/116.0.5845.96/chromedriver unexpectedly exited. status code was: 255
  • 添加chromium-driver后新错误:unknown error: Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist)

环境与配置信息

版本信息

  • Python 3.10
  • Selenium 4.11.2
  • pytest 7.4.0

Dockerfile

# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.10-slim

# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y wget gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

CMD ["python3", "-m", "pytest", "--ignore=./tests", "--browser", "headless"]

浏览器配置代码

options = Chrome_Options()
options.add_argument("--headless")
options.add_argument("--incognite")
options.add_argument("--disable-gpu")
options.add_argument("--single-process")
options.add_argument("--disable-extensions")
options.add_argument('--disable-dev-shm-usage')
options.add_argument("disable-infobars")
options.add_argument("start-maximized")
service = Service()
driver = webdriver.Chrome(
    service=service,
    options=options,
)

错误栈详情

初始错误栈

---------------------------- Captured stderr setup -----------------------------
Traceback (most recent call last):
  File "/app/testsuite/conftest.py", line 161, in init_driver
    driver = webdriver.Chrome(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 53, in __init__
    self.service.start()
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 109, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 122, in assert_process_still_running
    raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /root/.cache/selenium/chromedriver/linux64/116.0.5845.96/chromedriver unexpectedly exited. Status code was: 255

添加chromium-driver后的错误栈

---------------------------- Captured stderr setup -----------------------------
Traceback (most recent call last):
  File "/app/testsuite/conftest.py", line 161, in init_driver
    driver = webdriver.Chrome(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 56, in __init__
    super().__init__(
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 206, in __init__
    self.start_session(capabilities)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 290, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 345, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /root/.cache/selenium/chrome/linux64/116.0.5845.96/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0xaaaab79ad8e0 <unknown>
#1 0xaaaab770876c <unknown>
#2 0xaaaab772e6c4 <unknown>
#3 0xaaaab772b814 <unknown>
#4 0xaaaab7766968 <unknown>
#5 0xaaaab77343c4 <unknown>
#6 0xaaaab77357dc <unknown>
#7 0xaaaab7979680 <unknown>
#8 0xaaaab797c4f4 <unknown>
#9 0xaaaab797c0c4 <unknown>
#10 0xaaaab7982e74 <unknown>
#11 0xaaaab797cc0c <unknown>
#12 0xaaaab7957ea0 <unknown>
#13 0xaaaab7995ae8 <unknown>
#14 0xaaaab7995cf8 <unknown>
#15 0xaaaab79a407c <unknown>
#16 0xffff944cee18 <unknown>
#17 0xffff94537e9c <unknown>

------------------------------ Captured log setup ------------------------------
WARNING  selenium.webdriver.common.selenium_manager:selenium_manager.py:133 The chromedriver version (116.0.5845.110) detected in PATH at /usr/bin/chromedriver might not be compatible with the detected chrome version (116.0.5845.96); currently, chromedriver 116.0.5845.96 is recommended for chrome 116.*, so it is advised to delete the driver in PATH and retry

解决方案

1. 修复版本不兼容问题

从警告日志可知,chromedriver版本116.0.5845.110与Chrome版本116.0.5845.96不匹配:

  • 删除通过apt-get install chromium-driver安装的驱动,避免Selenium混淆调用
  • 依赖Selenium 4+自带的Selenium Manager自动匹配对应版本的ChromeDriver,无需手动安装第三方驱动

2. 调整Chrome启动参数

修正浏览器配置中的错误参数,添加必要启动项:

options = ChromeOptions()
# 修正拼写错误:--incognite 改为 --incognito
options.add_argument("--incognito")
# 使用Selenium 4.10+推荐的无头模式,兼容性更好
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
# 移除--single-process,容器环境下该参数易导致Chrome崩溃
options.add_argument("--disable-extensions")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-infobars")
# 容器中无法最大化窗口,改为设置固定分辨率
options.add_argument("--window-size=1920,1080")
# 解决DevToolsActivePort问题的核心参数
options.add_argument("--no-sandbox")
options.add_argument("--remote-debugging-port=9222")
# 指定本地安装的Chrome路径,避免Selenium自动下载版本冲突
options.binary_location = "/usr/bin/google-chrome"
# 允许root用户运行Chrome
options.add_argument("--allow-running-insecure-content")

service = Service()
driver = webdriver.Chrome(
    service=service,
    options=options,
)

3. 优化Dockerfile

精简依赖安装,避免冗余包:

FROM python:3.10-slim

# 安装Chrome必要依赖
RUN apt-get update && apt-get install -y --no-install-recommends \
    wget \
    gnupg \
    libasound2 \
    libatk-bridge2.0-0 \
    libdrm2 \
    libxkbcommon0 \
    libgbm1 \
    libnss3 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    xdg-utils \
    fonts-liberation \
    && rm -rf /var/lib/apt/lists/*

# 安装Chrome稳定版
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
    && apt-get update && apt-get install -y google-chrome-stable \
    && rm -rf /var/lib/apt/lists/*

# 安装Python依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 设置工作目录
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .

# 运行测试
CMD ["python3", "-m", "pytest", "--ignore=./tests", "--browser", "headless"]

4. 验证步骤

  1. 重新构建镜像:docker build -t selenium-test .
  2. 运行容器:docker run --rm selenium-test
  3. 查看测试输出,确认错误是否消除

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 23:47:32