You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS服务器上使用Selenium时Chromium二进制文件未找到的问题求助

MacOS服务器上使用Selenium时Chromium二进制文件未找到的问题求助

大家好,我在MacOS服务器上用Selenium做自动化测试,已经安装了Chrome和Chromedriver,但运行代码时一直报找不到二进制文件的错误,折腾了好久都没解决,想请大家帮忙看看问题出在哪。

我的代码情况

这是我写的Chrome初始化代码:

options = webdriver.ChromeOptions()

if(env['HEADLESS']):
    options.add_argument('--headless')
options.add_argument('--auto-show-cursor')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

try:
    driver = webdriver.Chrome('chromedriver',options=options)
except WebDriverException:
    # Exception occurred, try using the specified executable_path
    chrome_binary_path = '/usr/bin/chromedriver'
    options.binary_location = chrome_binary_path
    driver = webdriver.Chrome('chromedriver', options=options)
    # 我之前也试过 driver = webdriver.Chrome(executable_path = chrome_binary_path, options=options)

遇到的错误日志

运行后抛出了两次WebDriverException,第一次的错误信息是:

Traceback (most recent call last):
File "/home/docker/web-testing-wtai/WTAI-project/src/BL/browsers.py", line 21, in chrome
driver = webdriver.Chrome('chromedriver',options=options)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
super().__init__(
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
super().__init__(
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/chromium/2529/usr/lib/chromium-browser/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x560c936c76a3 <unknown>
#1 0x560c9340cad6 <unknown>
#2 0x560c934354c6 <unknown>
#3 0x560c934318a0 <unknown>
#4 0x560c9346f78d <unknown>
#5 0x560c9346ef6f <unknown>
#6 0x560c93466993 <unknown>
#7 0x560c9343c414 <unknown>
#8 0x560c9343d47e <unknown>
#9 0x560c9368aacd <unknown>
#10 0x560c9368f505 <unknown>
#11 0x560c93698a0e <unknown>
#12 0x560c9368ff8c <unknown>
#13 0x560c93660a62 <unknown>
#14 0x560c936b1538 <unknown>
#15 0x560c936b16dc <unknown>
#16 0x560c936c0b35 <unknown>
#17 0x7f8232ae6b43 <unknown>

然后在异常处理块里又抛出了第二次错误:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/docker/web-testing-wtai/WTAI-project/src/BL/utils.py", line 418, in initializeBrowser
driver = browsers.chrome(env)
File "/home/docker/web-testing-wtai/WTAI-project/src/BL/browsers.py", line 26, in chrome
driver = webdriver.Chrome('chromedriver', options=options)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chrome/webdriver.py", line 80, in __init__
super().__init__(
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/chromium/webdriver.py", line 104, in __init__
super().__init__(
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
self.start_session(capabilities, browser_profile)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "/home/docker/web-testing-wtai/WTAI-project/env/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: no chrome binary at /usr/bin/chromedriver

我已经做的排查

我检查了/usr/bin/chromedriver的内容,用nano /usr/bin/chromedriver打开后看到是个shell脚本:

#!/bin/sh
if ! [ -x /snap/bin/chromium.chromedriver ]; then
    echo "" >&2
    echo "Command '$0' requires the chromium snap to be installed." >&2
    echo "Please install it with:" >&2
    echo "" >&2
    echo "snap install chromium" >&2
    echo "" >&2
    exit 1
fi
exec /snap/bin/chromium.chromedriver "$@"

我现在完全搞不懂为什么Selenium找不到Chromedriver的可执行文件,到底哪里写错了?另外有没有办法不用手动指定路径,像我之前在旧电脑上那样直接运行?还有,我怀疑是不是Chromedriver和Chromium版本不匹配?旧电脑上是Chromium 114.0.5735.90,现在新电脑是114.0.5735.198,Selenium版本都是4.8.2。如果是版本问题的话,怎么用命令行来更新或切换Chromedriver版本呢?

备注:内容来源于stack exchange,提问作者pepito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:08:11