使用Docker Compose运行Selenium无法创建会话求助
问题:Selenium Chrome Standalone Docker容器无法创建会话
尝试连接selenium/standalone-chrome Docker容器时,无法创建会话,持续出现如下错误:
21:02:35.263 WARN [SeleniumSpanExporter$1.lambda$export$3] - {"traceId": "18a8e2283000ca2a77b5526145bdce62","eventTime": 1719867755250989883,"eventName": "exception","attributes": {"exception.message": "Unable to create session: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: crashed. (disconnected: unable to connect to renderer) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Host info: host: 'chrome', ip: '172.19.0.2' Build info: version: '4.22.0', revision: 'c5f3146703' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11' Driver info: driver.version: unknown Build info: version: '4.22.0', revision: 'c5f3146703' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11' Driver info: driver.version: unknown","exception.stacktrace": "org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: session not created: Chrome failed to start: crashed. (disconnected: unable to connect to renderer) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) Host info: host: 'chrome', ip: '172.19.0.2' Build info: version: '4.22.0', revision: 'c5f3146703' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11' Driver info: driver.version: unknown Build info: version: '4.22.0', revision: 'c5f3146703' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11' Driver info: driver.version: unknown at org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:221) at org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:71) at org.openqa.selenium.grid.node.local.SessionSlot.apply(SessionSlot.java:147) at org.openqa.selenium.grid.node.local.LocalNode.newSession(LocalNode.java:469) at org.openqa.selenium.grid.distributor.local.LocalDistributor.startSession(LocalDistributor.java:652) at org.openqa.selenium.grid.distributor.local.LocalDistributor.newSession(LocalDistributor.java:571) at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.handleNewSessionRequest(LocalDistributor.java:834) at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.lambda$run$1(LocalDistributor.java:791) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.base/java.lang.Thread.run(Unknown Source) ","exception.type": "org.openqa.selenium.SessionNotCreatedException","logger": "org.openqa.selenium.grid.distributor.local.LocalDistributor","request.payload": "[Capabilities {browserName: chrome, goog:chromeOptions: {args: [--headless, --no-sandbox, --disable-dev-shm-usage], extensions: []}, goog:loggingPrefs: {performance: ALL}, pageLoadStrategy: normal}]"}}
我的docker-compose.yml配置:
services: chrome: platform: linux/x86_64 hostname: chrome image: selenium/standalone-chrome:4.22.0-20240621 shm_size: 2gb privileged: true datafetcher: build: . depends_on: - chrome
触发错误的代码:
options = Options() options.set_capability("goog:loggingPrefs", {"performance": "ALL"}) options.add_argument("--headless") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") driver = webdriver.Remote("http://chrome:4444/wd/hub", options=options)
已尝试更换不同版本的selenium/chrome-standalone镜像,以及改用node/hub容器替代独立容器,但问题仍未解决。
解决方案建议
- 更新无头模式参数:新版Chrome的
--headless参数已被废弃,改用--headless=new,这是容器环境中启动Chrome无头模式的标准参数 - 补充Chrome启动参数:添加
--disable-gpu、--window-size=1920,1080,解决渲染窗口尺寸不足、GPU相关的启动问题 - 配置容器健康检查:确保Chrome服务完全就绪后再发起连接,修改docker-compose.yml:
services: chrome: platform: linux/x86_64 hostname: chrome image: selenium/standalone-chrome:4.22.0-20240621 shm_size: 2gb privileged: true healthcheck: test: ["CMD", "curl", "-f", "http://localhost:4444/wd/hub/status"] interval: 5s timeout: 10s retries: 5 datafetcher: build: . depends_on: chrome: condition: service_healthy - 对齐Selenium版本:确保代码中使用的Selenium版本与容器内的版本(4.22.0)一致,避免版本不匹配导致的兼容性问题
- 添加调试参数:在Chrome启动参数中加入
--remote-debugging-port=9222,便于排查Chrome启动时的底层问题 - 查看容器日志:执行
docker logs <你的chrome容器ID>,获取Chrome启动失败的具体细节,定位核心原因
内容的提问来源于stack exchange,提问作者Sam Ehlers
相关产品推荐
相关产品推荐

