Docker中运行Selenium Python UI测试时Chrome崩溃问题求助
问题描述
运行基于Selenium、Python和ChromeDriver的UI测试用例时,出现错误提示:
The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed
测试用例在本地运行正常,但Docker环境下执行失败。相关技术栈信息:
- Selenium: 4.11.2
- Python: 3.10(Dockerfile中使用了3.9)
- ChromeDriver: 116.0.5845.96
- Chrome Binary: 116.0.5845.96
已尝试的调试方法:
- 让Selenium Manager自动安装驱动和二进制文件,问题依旧存在
- 手动指定ChromeDriver路径,未解决崩溃问题
- 调整ChromeOptions参数、降低Selenium和驱动版本,无效
Dockerfile内容
RUN pip install logging-formatter-anticrlf FROM --platform=linux/amd64 python:3.9 # Chrome dependency WORKDIR /usr/bin RUN apt-get update && apt-get install -y \ fonts-liberation \ libasound2 \ libatk-bridge2.0-0 \ libatk1.0-0 \ libatspi2.0-0 \ libcups2 \ libdbus-1-3 \ libdrm2 \ libgbm1 \ libgtk-3-0 \ libgtk-4-1 \ libnspr4 \ libnss3 \ libwayland-client0 \ libxcomposite1 \ libxdamage1 \ libxfixes3 \ libxkbcommon0 \ libxrandr2 \ xdg-utils \ libu2f-udev \ libvulkan1 \ software-properties-common \ unzip \ curl \ xvfb # Chrome instalation RUN wget -q https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_116.0.5845.96-1_amd64.deb RUN apt --fix-broken install RUN apt-get install ./google-chrome-stable_116.0.5845.96-1_amd64.deb -y RUN echo "Chrome: " && google-chrome --version WORKDIR /module COPY . . COPY setup.py setup.py COPY entrypoint.sh entrypoint.sh COPY README.md README.md RUN pip install webdriver-manager==4.0.0 RUN pip install selenium==4.11.2 RUN pip install chromedriver-autoinstaller RUN pip install selenium --user RUN pip install py3chrome RUN wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip RUN unzip chromedriver-linux64.zip RUN mv chromedriver-linux64/chromedriver /usr/bin/chromedriver RUN chmod 777 /usr/bin/chromedriver RUN chmod 777 /opt/google/chrome/google-chrome RUN pytest -k test_case
测试用例代码
def test_case(): from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService, Service from selenium.webdriver.common.by import By chrome_options = webdriver.ChromeOptions() #s = Service(executable_path=driver_path) chrome_options.add_argument('--headless') chrome_options.add_argument('--privileged') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument("--disable-setuid-sandbox") chrome_options.add_argument('--ignore-certificate-errors') chrome_options.add_argument('--disable-dev-shm-usage')# overcome limited resource problems chrome_options.add_argument('--disable-extensions') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--remote-allow-origins=*") chrome_options.add_argument("--remote-debugging-port=9222") # this chrome_options.add_experimental_option( "prefs", {"profile.default_content_setting_values.notifications": 1} ) driver = webdriver.Chrome(options=chrome_options) driver.get(“www.google.com”)
解决方案
1. 修复Dockerfile无效指令与版本不一致问题
- 开头的
RUN pip install logging-formatter-anticrlf写在FROM之前,属于无效指令,需移到FROM之后的层级中 - 统一Python版本:将Dockerfile中的
python:3.9改为python:3.10,与本地环境保持一致,避免版本差异引发的兼容性问题
2. 精简Chrome安装与驱动管理逻辑
- 安装Chrome后清理安装包:添加
RUN rm google-chrome-stable_116.0.5845.96-1_amd64.deb,减少镜像冗余 - 移除重复的驱动管理工具:
webdriver-manager、chromedriver-autoinstaller和手动下载ChromeDriver重复,保留Selenium 4.x自带的Selenium Manager即可,无需手动下载驱动
3. 优化ChromeOptions参数
- Chrome 112+版本推荐使用
--headless=new替代旧的--headless,旧无头模式在Docker环境下兼容性较差 - 移除不必要的参数:
--privileged是容器级权限,测试场景不需要;--remote-debugging-port=9222会占用端口导致Chrome启动异常 - 补全URL协议头:将
driver.get(“www.google.com”)改为driver.get("https://www.google.com"),缺少协议头会导致Chrome无法加载页面进而崩溃
4. 调整Dockerfile执行顺序(利用缓存)
将系统依赖安装、Chrome安装、Python依赖安装等步骤放在代码复制之前,避免每次修改代码都重新安装依赖,提升构建效率
修复后的Dockerfile示例
FROM --platform=linux/amd64 python:3.10 # 安装系统依赖 RUN apt-get update && apt-get install -y \ fonts-liberation \ libasound2 \ libatk-bridge2.0-0 \ libatk1.0-0 \ libatspi2.0-0 \ libcups2 \ libdbus-1-3 \ libdrm2 \ libgbm1 \ libgtk-3-0 \ libnspr4 \ libnss3 \ libwayland-client0 \ libxcomposite1 \ libxdamage1 \ libxfixes3 \ libxkbcommon0 \ libxrandr2 \ xdg-utils \ libu2f-udev \ libvulkan1 \ unzip \ curl \ && rm -rf /var/lib/apt/lists/* # 安装Chrome RUN wget -q https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_116.0.5845.96-1_amd64.deb \ && apt-get install ./google-chrome-stable_116.0.5845.96-1_amd64.deb -y \ && rm google-chrome-stable_116.0.5845.96-1_amd64.deb \ && google-chrome --version # 设置工作目录 WORKDIR /module # 安装Python依赖 COPY requirements.txt . RUN pip install --no-cache-dir selenium==4.11.2 logging-formatter-anticrlf # 复制代码文件 COPY . . # 运行测试 CMD ["pytest", "-k", "test_case"]
修复后的测试用例代码
def test_case(): from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless=new') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-setuid-sandbox') chrome_options.add_argument('--ignore-certificate-errors') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-extensions') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("--remote-allow-origins=*") chrome_options.add_experimental_option( "prefs", {"profile.default_content_setting_values.notifications": 1} ) # Selenium 4.x自动通过Selenium Manager匹配ChromeDriver driver = webdriver.Chrome(options=chrome_options) try: driver.get("https://www.google.com") # 可添加断言验证页面加载成功 assert "Google" in driver.title finally: driver.quit()
额外调试提示
- 若仍出现崩溃问题,可尝试添加
--disable-web-security和--disable-features=VizDisplayCompositor参数 - Docker容器资源有限,运行时可增加内存分配:
docker run --memory=2g ...
内容的提问来源于stack exchange,提问作者Hima
相关产品推荐
相关产品推荐

