我一直在尝试连接到 selenium/chrome-standalone 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.\n (disconnected: unable to connect to renderer)\n (The process started from chrome location \u002fusr\u002fbin\u002fgoogle-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) \nHost info: host: 'chrome', ip: '172.19.0.2'\nBuild info: version: '4.22.0', revision: 'c5f3146703'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11'\nDriver info: driver.version: unknown\nBuild info: version: '4.22.0', revision: 'c5f3146703'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11'\nDriver 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.\n (disconnected: unable to connect to renderer)\n (The process started from chrome location \u002fusr\u002fbin\u002fgoogle-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) \nHost info: host: 'chrome', ip: '172.19.0.2'\nBuild info: version: '4.22.0', revision: 'c5f3146703'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11'\nDriver info: driver.version: unknown\nBuild info: version: '4.22.0', revision: 'c5f3146703'\nSystem info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.31-linuxkit', java.version: '17.0.11'\nDriver info: driver.version: unknown\n\tat org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:221)\n\tat org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:71)\n\tat org.openqa.selenium.grid.node.local.SessionSlot.apply(SessionSlot.java:147)\n\tat org.openqa.selenium.grid.node.local.LocalNode.newSession(LocalNode.java:469)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor.startSession(LocalDistributor.java:652)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor.newSession(LocalDistributor.java:571)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.handleNewSessionRequest(LocalDistributor.java:834)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.lambda$run$1(LocalDistributor.java:791)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\n\tat java.base\u002fjava.lang.Thread.run(Unknown Source)\n","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 并使用节点/集线器容器而不是独立容器。有什么建议吗?
问题来自名为
SE_NODE_MAX_SESSIONS
的变量。 在这里您可以阅读更多内容。这是修复方法:
services:
chrome:
platform: linux/x86_64
hostname: chrome
image: selenium/standalone-chrome:4.22.0-20240621
shm_size: 2gb
privileged: true
environment:
SE_NODE_MAX_SESSIONS: 4
SE_NODE_OVERRIDE_MAX_SESSIONS: "true"
datafetcher:
build: .
depends_on:
- chrome