Docker运行Selenium Python UI测试时Chrome崩溃报错求助
问题:Docker中运行Selenium+ChromeDriver时Chrome进程崩溃
在Docker环境运行基于Selenium、Python和ChromeDriver的UI测试时,报错:
启动位置为/opt/chrome/chrome的Chrome进程已终止,ChromeDriver判定Chrome崩溃
本地运行测试无异常,仅Docker环境出现该问题。
技术栈信息
- Selenium: 4.12.0
- Python版本: 3.10
- Chrome Driver版本:
117.0.5938.88 - Chrome Binary版本:
117.0.5938.88
Dockerfile
FROM public.ecr.aws/lambda/python:3.9.2023.04.17.20 as build RUN yum install -y unzip && \ curl -Lo "/tmp/chromedriver-linux64.zip" "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.88/linux64/chromedriver-linux64.zip" && \ curl -Lo "/tmp/chrome-linux64.zip" "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.88/linux64/chrome-linux64.zip" && \ unzip /tmp/chromedriver-linux64.zip -d /opt/ && \ unzip /tmp/chrome-linux64.zip -d /opt/ FROM public.ecr.aws/lambda/python:3.9.2023.04.17.20 RUN yum install -q atk cups-libs gtk3 libXcomposite alsa-lib \ libXcursor libXdamage libXext libXi libXrandr libXScrnSaver \ libXtst pango at-spi2-atk libXt xorg-x11-server-Xvfb \ xorg-x11-xauth dbus-glib dbus-glib-devel -y COPY --from=build /opt/chrome-linux64 /opt/chrome COPY --from=build /opt/chromedriver-linux64 /opt/ COPY pyproject.toml ./ COPY src ./src COPY ./modernca.cer ./artifactory_ng.crt COPY ./requirementss.txt ./ COPY ./.netrc /root/.netrc RUN chmod 600 /root/.netrc RUN CERT_PATH="./artifactory_ng.crt" NODE_EXTRA_CA_CERTS=${CERT_PATH} REQUESTS_CA_BUNDLE=${CERT_PATH} NO_VERIFY_SSL=true pip install -q -r requirementss.txt RUN pip install datadog-lambda ddtrace RUN rm -rf artifactory_ng.crt RUN rm -rf /root/.netrc RUN yum update -y libcurl curl python-libs python libxml2 openssl-libs openssl nss-sysinit nss nss-tools libdb-utils libdb cpio ca-certificates COPY --from=public.ecr.aws/datadog/lambda-extension:latest /opt/extensions/ /opt/extensions #CMD [ "datadog_lambda.handler.handler" ] CMD [ "src.lambdas.ui_automation.handler" ]
现有Selenium驱动代码
from seleniumwire.webdriver import ChromeOptions from selenium.webdriver.chrome.service import Service options = ChromeOptions() options.binary_location = '/opt/chrome/chrome' chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_driver_service = Service(executable_path='/opt/chromedriver') driver = webdriver.Chrome(service=chrome_driver_service, options=options )
修复方案
1. 修正代码变量名错误
代码中定义了options = ChromeOptions(),但后续添加参数时误用了未定义的chrome_options变量,导致配置的启动参数完全未生效,直接引发Chrome启动失败。同时需补充webdriver的导入,修正后的代码:
from seleniumwire.webdriver import ChromeOptions from selenium.webdriver.chrome.service import Service from seleniumwire import webdriver options = ChromeOptions() options.binary_location = '/opt/chrome/chrome' # 统一使用options变量配置参数 options.add_argument('--headless=new') # 推荐使用Chrome 112+支持的新无头模式 options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') # 解决无头模式下的GPU渲染兼容性问题 options.add_argument('--window-size=1920,1080') # 指定窗口尺寸,避免渲染异常 # 修正chromedriver路径:Docker中解压后实际路径为/opt/chromedriver-linux64/chromedriver chrome_driver_service = Service(executable_path='/opt/chromedriver-linux64/chromedriver') driver = webdriver.Chrome(service=chrome_driver_service, options=options)
2. 修正ChromeDriver路径
从Dockerfile的解压逻辑来看,chromedriver-linux64.zip解压后会在/opt/下生成chromedriver-linux64目录,驱动文件实际路径是/opt/chromedriver-linux64/chromedriver,而非代码中写的/opt/chromedriver,路径错误会导致驱动无法找到,间接引发Chrome崩溃。
3. 补充Chrome运行依赖
在Dockerfile的依赖安装命令中,补充libdrm和libgbm两个库,它们是Chrome无头模式运行的必要依赖:
RUN yum install -q atk cups-libs gtk3 libXcomposite alsa-lib \ libXcursor libXdamage libXext libXi libXrandr libXScrnSaver \ libXtst pango at-spi2-atk libXt xorg-x11-server-Xvfb \ xorg-x11-xauth dbus-glib dbus-glib-devel libdrm libgbm -y
4. 确保Chrome二进制文件权限
在Dockerfile中添加权限设置命令,保证Chrome可执行文件有运行权限:
COPY --from=build /opt/chrome-linux64 /opt/chrome RUN chmod +x /opt/chrome/chrome
内容的提问来源于stack exchange,提问作者Manspof
相关产品推荐
相关产品推荐

