selenium/standalone-chrome+Java17环境下WebDriver异常求排查修复
Selenium Chrome Docker环境问题排查与修复
场景说明
基于selenium/standalone-chrome:115.0镜像构建集成Java17的Docker环境,相关代码如下:
Dockerfile
FROM selenium/standalone-chrome:115.0 RUN wget -O- https://apt.corretto.aws/corretto.key | sudo apt-key add - RUN sudo apt update # RUN sudo apt upgrade RUN sudo apt-get -y install software-properties-common RUN sudo add-apt-repository 'deb https://apt.corretto.aws stable main' RUN sudo apt-get update RUN sudo apt-get install -y java-17-amazon-corretto-jdk ENV CHROMEDRIVER_PORT 4444 ENV CHROMEDRIVER_WHITELISTED_IPS "127.0.0.1" ENV CHROMEDRIVER_URL_BASE '' EXPOSE 4444 EXPOSE 8080 ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar # For Testing ENTRYPOINT ["java","-jar", "-Xmx600m","/app.jar"]
Java代码
// System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); ChromeOptions chromeOptions = new ChromeOptions(); // chromeOptions.setBinary("/usr/local/bin/chrome"); chromeOptions.addArguments("--headless=new"); chromeOptions.addArguments("--no-sandbox"); chromeOptions.addArguments("--disable-gpu"); chromeOptions.addArguments("--remote-allow-origins=*"); WebDriver webDriver = new ChromeDriver(chromeOptions); webDriver.get("https://www.google.com/");
出现的错误
错误1:ChromeDriver版本不兼容
The chromedriver version (115.0.5790.102) detected in PATH at /usr/bin/chromedriver might not be compatible with the detected chrome version (115.0.5790.110); currently, chromedriver 115.0.5790.170 is recommended for chrome 115.*, so it is advised to delete the driver in PATH and retry
错误2:页面崩溃导致会话删除
org.openqa.selenium.WebDriverException: unknown error: session deleted because of page crash from tab crashed (Session info: chrome=115.0.5790.110) Build info: version: '4.11.0', revision: '040bc5406b' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit', java.version: '17.0.8.1' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [29d1fa15d0fab05c1c479470b82aaa69, get {url=https://www.kcrg.com/2022/02/08/john-deere-acquires-majority-ownership-battery-manufacturer/}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 115.0.5790.110, chrome: {chromedriverVersion: 115.0.5790.102 (90efd4b0ad6..., userDataDir: /tmp/.org.chromium.Chromium...}, goog:chromeOptions: {debuggerAddress: localhost:43301}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:43301/devtoo..., se:cdpVersion: 115.0.5790.110, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
修复方案
针对错误1:版本不兼容问题
问题根源是Docker镜像中存在两个版本的chromedriver:/usr/bin/chromedriver(版本115.0.5790.102)和selenium/standalone-chrome自带的/usr/local/bin/chromedriver(与Chrome版本匹配),系统优先找到了旧版本。
有两种修复方式:
- 调整PATH优先级:在Dockerfile中添加环境变量,让
/usr/local/bin优先于/usr/bin,确保使用镜像自带的正确版本:ENV PATH="/usr/local/bin:$PATH" - 替换为推荐版本:如果需要严格使用官方推荐的115.0.5790.170版本,在Dockerfile中添加以下步骤:
# 删除旧的chromedriver RUN sudo rm -f /usr/bin/chromedriver # 下载并替换为指定版本 RUN wget -O /tmp/chromedriver.zip https://storage.googleapis.com/chrome-for-testing-public/115.0.5790.170/linux64/chromedriver-linux64.zip RUN unzip /tmp/chromedriver.zip -d /tmp RUN sudo mv /tmp/chromedriver-linux64/chromedriver /usr/bin/chromedriver RUN sudo chmod +x /usr/bin/chromedriver RUN rm -rf /tmp/chromedriver.zip /tmp/chromedriver-linux64
针对错误2:页面崩溃问题
该问题通常是Chrome在headless模式下资源不足或环境配置导致,可通过以下步骤修复:
- 添加Chrome启动参数:在Java代码的ChromeOptions中补充以下参数:
chromeOptions.addArguments("--disable-dev-shm-usage"); // 禁用共享内存,避免容器内存限制导致崩溃 chromeOptions.addArguments("--window-size=1920,1080"); // 设置固定窗口大小,避免渲染异常 chromeOptions.addArguments("--disable-extensions"); // 禁用扩展,减少资源占用 - 调整Java进程内存:在Dockerfile的ENTRYPOINT中增大Java堆内存,比如从
-Xmx600m改为-Xmx1g:ENTRYPOINT ["java","-jar", "-Xmx1g","/app.jar"] - 增加Docker容器内存:启动容器时指定内存限制,比如:
docker run --memory=2g your-image-name
内容的提问来源于stack exchange,提问作者HeronAlgoSearch
相关产品推荐
相关产品推荐

