docker部署Selenium Grid,创建任务时无法创建session

问题

docker部署Selenium Grid,创建任务时无法创建session

报错信息

/Users/ann/PycharmProjects/pythonDailyProject/.venv/bin/python /Users/ann/PycharmProjects/pythonDailyProject/web_atuo/selenium_grid_demo.py 
Traceback (most recent call last):
  File "/Users/ann/PycharmProjects/pythonDailyProject/web_atuo/selenium_grid_demo.py", line 25, in <module>
    driver = webdriver.Remote(
             ^^^^^^^^^^^^^^^^^
  File "/Users/ann/PycharmProjects/pythonDailyProject/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 212, in __init__
    self.start_session(capabilities)
  File "/Users/ann/PycharmProjects/pythonDailyProject/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 299, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ann/PycharmProjects/pythonDailyProject/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in execute
    self.error_handler.check_response(response)
  File "/Users/ann/PycharmProjects/pythonDailyProject/.venv/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Could not start a new 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: disconnected: Unable to receive message from renderer
  (failed to check if window was closed: disconnected: not connected to DevTools)
  (Session info: chrome=129.0.6668.100) 
Host info: host: '707fdad0f52f', ip: '172.18.0.3'
Build info: version: '4.25.0', revision: '030fcf7918'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.26-linuxkit', java.version: '17.0.12'
Driver info: driver.version: unknown
Build info: version: '4.25.0', revision: '030fcf7918'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '6.6.26-linuxkit', java.version: '17.0.12'
Driver info: driver.version: unknown
Build info: version: '4.25.0', revision: '030fcf7918'
System info: os.name: 'Linux', os.arch: 'aarch64', os.version: '6.6.26-linuxkit', java.version: '17.0.12'
Driver info: driver.version: unknown
Stacktrace:
    at org.openqa.selenium.grid.node.remote.RemoteNode.newSession (RemoteNode.java:157)
    at org.openqa.selenium.grid.distributor.local.LocalDistributor.startSession (LocalDistributor.java:654)
    at org.openqa.selenium.grid.distributor.local.LocalDistributor.newSession (LocalDistributor.java:573)
    at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.handleNewSessionRequest (LocalDistributor.java:836)
    at org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.lambda$run$1 (LocalDistributor.java:793)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:635)
    at java.lang.Thread.run (Thread.java:840)

Process finished with exit code 1

环境

启动命令,根据官网提示配置

docker network create grid

docker run -d -p 4442-4444:4442-4444 --net grid --name hub selenium/hub:latest

docker run -d --net grid -e SE_EVENT_BUS_HOST=hub \
    --shm-size="2g" \
    -e SE_EVENT_BUS_PUBLISH_PORT=4442 \
    -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 \
    selenium/node-chrome:latest


脚本代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

hub_url = 'http://10.1.6.65:4444'
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
driver = webdriver.Remote(
    command_executor=hub_url,
    options=chrome_options  # 使用 options 代替 desired_capabilities
)
driver.get("https://www.baidu.com")
time.sleep(2)
print(driver.title)
print(driver.page_source)
driver.quit()

问题解决

乌龙事件,没仔细查看官网说明文档

selenium/node-chrome 并不支持arm64也就是m芯片的mac平台,因为docker部署容器,只虚拟化应用并未虚拟化内核。

更换为selenium/node-chromium 镜像,重新部署环境

执行成功

使用docker compose部署镜像

docker-compose.yml

version: "3"
services:
  chromium:
    image: seleniarm/node-chromium:latest
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: seleniarm/node-firefox:latest
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: seleniarm/hub:latest
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"