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

EC2实例运行Selenium Python脚本报Chrome not reachable错误求助

解决AWS EC2(Red Hat 8.7)上Selenium Chrome "chrome not reachable" 错误

环境信息

  • Google Chrome:100.0.4896.60
  • ChromeDriver:100.0.4896.60
  • 操作系统:Red Hat 8.7
  • Python:3.6.8
  • Selenium:3.141.0
  • Pytest:7.0.1

问题重现

运行以下测试脚本时抛出selenium.common.exceptions.WebDriverException: Message: chrome not reachable错误:

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

#argument to switch off suid sandBox and no sandBox in Chrome
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument("--remote-debugging-port=9222")
# chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--headless')
#chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument("--incognito")
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36"
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument('ignore-certificate-errors')


driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
driver.get('http://www.google.com')
print(driver.title)
driver.quit()

错误日志:

================================================================================================================== ERRORS ===================================================================================================================
_________________________________________________________________________________________________________ ERROR collecting main.py __________________________________________________________________________________________________________
main.py:21: in <module>
    driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py:81: in __init__
    desired_capabilities=desired_capabilities)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:157: in __init__
    self.start_session(capabilities, browser_profile)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:252: in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py:321: in execute
    self.error_handler.check_response(response)
/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py:242: in check_response
    raise exception_class(message, screen, stacktrace)
E   selenium.common.exceptions.WebDriverException: Message: chrome not reachable
========================================================================================================== short test summary info ==========================================================================================================
ERROR main.py - selenium.common.exceptions.WebDriverException: Message: chrome not reachable
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

解决方案

1. 确认Chrome二进制文件路径

降级Chrome后,可执行文件路径可能发生变化,先通过终端命令确认:

which google-chrome

得到路径后,在ChromeOptions中显式指定:

chrome_options.binary_location = "/usr/bin/google-chrome"  # 替换为实际输出的路径

2. 移除冲突的启动参数

--remote-debugging-port=9222在无桌面环境的EC2 headless模式下容易导致连接问题,直接注释或删除该参数。

3. 安装Chrome依赖库

Red Hat 8.7默认缺少部分Chrome运行所需的图形依赖,执行以下命令安装:

sudo yum install -y libX11 libXext libXrender libXtst libXScrnSaver libxcb

4. 验证Chrome单独运行状态

在终端直接运行Chrome的headless命令,确认浏览器本身能正常工作:

google-chrome --headless --no-sandbox --disable-dev-shm-usage --dump-dom http://www.google.com

如果无法输出页面内容,说明Chrome降级不彻底,重新安装指定版本并禁止自动更新:

# 下载对应rpm包
wget https://dl.google.com/linux/chrome/rpm/stable/x86_64/google-chrome-stable-100.0.4896.60-1.x86_64.rpm
# 安装并锁定版本
sudo rpm -ivh google-chrome-stable-100.0.4896.60-1.x86_64.rpm
sudo yum-mark hold google-chrome-stable

5. 修改后的测试脚本

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
# 显式指定Chrome二进制路径
chrome_options.binary_location = "/usr/bin/google-chrome"
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-popup-blocking')
chrome_options.add_argument("--incognito")
userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36"
chrome_options.add_argument(f'user-agent={userAgent}')
chrome_options.add_argument('ignore-certificate-errors')

# 初始化WebDriver
driver = webdriver.Chrome("/usr/local/share/chromedriver", options=chrome_options)
driver.get('http://www.google.com')
print(driver.title)
driver.quit()

内容的提问来源于stack exchange,提问作者Cmmar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 15:30:23