Ubuntu环境下Java+Selenium启动ChromeDriver失败:无法创建新会话
问题:Docker中Selenium启动Chrome失败(崩溃)
环境信息
- 平台:Ubuntu 22.04
- Java版本:17.0.7
- Selenium版本:4.10.0
- Chrome及ChromeDriver版本:114.0.5735
尝试的两种启动方式
方式1:使用宿主机已安装的Chrome和ChromeDriver
测试代码:
System.setProperty("webdriver.chrome.driver", chromeWebdriver); ChromeOptions co = new ChromeOptions(); co.addArguments("--remote-allow-origins=*"); co.addArguments("--headless=new"); co.addArguments("--allowed-ips=127.0.0.1"); co.addArguments("--no-sandbox"); co.addArguments("--disable-setuid-sandbox"); co.addArguments("--remote-debugging-port=9222"); co.addArguments("--disable-dev-shm-usage"); co.addArguments("--disable-extensions"); co.addArguments("--disable-gpu"); driver = new ChromeDriver(co); driver.get(W3SCHOOL_URL); logger.info("test_chrome_by_system_property page title: {}", driver.getTitle());
方式2:通过Dockerfile安装Chrome和ChromeDriver
Dockerfile相关代码:
## 安装Google Chrome RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' RUN apt-get -y update RUN apt-get install -y google-chrome-stable ## 安装ChromeDriver RUN wget -q -O $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip RUN unzip $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip -d /usr/bin/ RUN chmod +x /usr/bin/chromedriver RUN rm -rf $APP_DIR/chromedriver_$CHROME_DRIVER_VERSION_linux64.zip
容器内版本验证结果:
docker exec -it my_docker_container_name google-chrome --help
Google Chrome 114.0.5735.198
docker exec -it my_docker_container_name chromedriver --version
ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})
错误信息
异常信息: 2023-07-12 13:15:05 [main] INFO c.e.maventest.MavenTestApplication.logStarted(61) - Started MavenTestApplication in 18.556 seconds (JVM running for 21.414) Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 45237 Only local connections are allowed. [1689167707.191][SEVERE]: bind() failed: Cannot assign requested address (99) ChromeDriver was started successfully. 2023-07-12 13:15:12 [main] ERROR c.e.m.service.SpringSeleniumRunner.run(101) - SpringSeleniumRunner exception: {} java.lang.Exception: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed. (unknown error: unable to discover open pages) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Build info: version: '4.1.4', revision: '535d840ee2' System info: host: '247af48ef4bc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit-pr', java.version: '17.0.7' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --headless=new, --allowed-ips=127.0.0.1, --no-sandbox, --disable-setuid-sandbox, --remote-debugging-port=9222, --disable-dev-shm-usage, --disable-extensions, --disable-gpu], extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [--remote-allow-origins=*, --headless=new, --allowed-ips=127.0.0.1, --no-sandbox, --disable-setuid-sandbox, --remote-debugging-port=9222, --disable-dev-shm-usage, --disable-extensions, --disable-gpu], extensions: []}}}] at com.example.maventest.service.SpringSeleniumRunner.testChromeBySystemProperty(SpringSeleniumRunner.java:60) at com.example.maventest.service.SpringSeleniumRunner.run(SpringSeleniumRunner.java:98) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:759) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:749) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) at com.example.maventest.MavenTestApplication.main(MavenTestApplication.java:13) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) Caused by: org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: crashed. (unknown error: unable to discover open pages) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Build info: version: '4.1.4', revision: '535d840ee2' System info: host: '247af48ef4bc', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.49-linuxkit-pr', java.version: '17.0.7'
解决建议
严格匹配Chrome与ChromeDriver版本
当前Chrome版本为114.0.5735.198,ChromeDriver为114.0.5735.90,小版本不一致会导致兼容性问题。下载对应Chrome版本的ChromeDriver替换现有版本。调整Chrome启动参数
- 移除
--allowed-ips=127.0.0.1,该参数会限制ChromeDriver的连接范围; - 移除
--remote-debugging-port=9222,避免端口绑定冲突; - 添加
--window-size=1920,1080,无头模式下必须指定窗口大小才能正常初始化; - 添加
--user-data-dir=/tmp/chrome-user-data,指定临时用户数据目录,避免权限问题。
- 移除
调整后的ChromeOptions代码:
ChromeOptions co = new ChromeOptions(); co.addArguments("--remote-allow-origins=*"); co.addArguments("--headless=new"); co.addArguments("--no-sandbox"); co.addArguments("--disable-setuid-sandbox"); co.addArguments("--disable-dev-shm-usage"); co.addArguments("--disable-extensions"); co.addArguments("--disable-gpu"); co.addArguments("--window-size=1920,1080"); co.addArguments("--user-data-dir=/tmp/chrome-user-data");
优化Docker容器配置
- 启动容器时添加
--shm-size=2g,增大共享内存,避免Chrome因内存不足崩溃; - 在Dockerfile中安装Chrome依赖库,添加以下命令:
RUN apt-get install -y libnss3 libgconf-2-4 libfontconfig1 libxrender1 libxss1
- 启动容器时添加
使用Selenium自动驱动管理
Selenium 4.x自带Selenium Manager,无需手动设置webdriver.chrome.driver,会自动匹配并下载对应版本的驱动,简化代码。
内容的提问来源于stack exchange,提问作者Dhamore
相关产品推荐
相关产品推荐

