M2设备运行Selenium时Chrome Driver启动后崩溃问题求助
问题描述
尝试使用以下两个镜像运行Selenium均出现异常:
seleniarm/standalone-chromiumselenium/standalone-chrome:107.0
启动异常日志
2023-03-23 11:04:16 2023-03-23 15:04:16,651 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2023-03-23 11:04:16 find: '/home/seluser/.config/chromium/Crash Reports/pending/': No such file or directory
应用访问报错
2023-03-23 13:29:00 17:29:00.712 WARN [SeleniumSpanExporter$1.lambda$export$1] - 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: unknown error: Chrome failed to start: crashed.
2023-03-23 13:29:00 (unknown error: DevToolsActivePort file doesn't exist)
2023-03-23 13:29:00 (The process started from chrome location /usr/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
当前使用ChromeDriver版本:111.0.5563.64
注:selenium/standalone-chrome:107.0镜像在M1用户中已稳定运行数月,队友使用相同配置可正常运行。
解决方案
1. 添加Chrome启动参数,修复DevTools端口问题
启动容器时,给Chrome配置无头模式及必要的容器兼容参数:
docker run -d -p 4444:4444 \ -e SE_CHROME_DRIVER_ARGS="--headless=new --no-sandbox --disable-dev-shm-usage --remote-debugging-port=9222" \ selenium/standalone-chrome:107.0
核心参数作用:
--headless=new:使用Chrome新版无头模式,兼容性更强--no-sandbox:关闭沙箱机制,适配容器环境--disable-dev-shm-usage:避免容器内共享内存不足导致崩溃--remote-debugging-port=9222:指定固定调试端口,确保DevToolsActivePort文件正常生成
2. 确保M1芯片的镜像架构匹配
M1为ARM64架构,需拉取对应架构的镜像:
docker pull --platform linux/arm64 selenium/standalone-chrome:107.0
拉取完成后再重新启动容器。
3. 清理容器缓存与残留资源
旧容器残留的配置或缓存可能引发冲突,执行以下命令清理:
docker stop $(docker ps -q --filter ancestor=selenium/standalone-chrome:107.0) docker rm $(docker ps -aq --filter ancestor=selenium/standalone-chrome:107.0) docker system prune -f
4. 匹配ChromeDriver与Chrome版本
当前使用的ChromeDriver版本(111.0.5563.64)与selenium/standalone-chrome:107.0内的Chrome版本(107)不兼容,需使用镜像自带的匹配版本ChromeDriver,或手动指定对应107版本的ChromeDriver。
5. 增加容器资源配额
M1芯片上容器资源不足可能导致Chrome崩溃,启动时指定内存和CPU限制:
docker run -d -p 4444:4444 --memory=2g --cpus=2 selenium/standalone-chrome:107.0
内容的提问来源于stack exchange,提问作者Guilherme

