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

EC2服务器上无法运行Selenium ChromeDriver问题求助

问题:ChromeDriver在Celery任务中运行时提示Chrome已崩溃,是否需要安装Chrome?

我用ChromeDriver开发了评论采集解析器,部署到服务器的Celery任务中运行时,出现如下错误:

(The process started from chrome location /home/ubuntu/.../chromedriver
is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

这个路径是ChromeDriverManager返回的,想问是否必须在服务器上安装并配置谷歌浏览器?我的Chrome选项配置代码如下:

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
# Adding argument to disable the AutomationControlled flag
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("start-maximized")
# Exclude the collection of enable-automation switches
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_argument("window-size=1280,800")

# Turn-off userAutomationExtension
chrome_options.add_experimental_option("useAutomationExtension", False)
path = ChromeDriverManager().install()
chrome_options.binary_location = path #"/home/ubuntu/chromedriver"
service = Service(path)
driver = webdriver.Chrome(service=service, options=chrome_options)

解答:
  • 必须安装Chrome浏览器:ChromeDriver只是驱动程序,本身不包含Chrome浏览器,它需要调用本地安装的Chrome二进制文件才能正常启动浏览器进程。你遇到的错误本质就是ChromeDriver找不到可执行的Chrome浏览器,导致启动失败后判定浏览器崩溃。
  • 代码存在关键错误:你把chrome_options.binary_location设置成了ChromeDriver的路径,这完全搞反了!这个参数是用来指定Chrome浏览器的安装路径(比如Ubuntu上默认路径是/usr/bin/google-chrome),而不是ChromeDriver的路径。
  • 具体修复步骤:
    1. 在Ubuntu服务器上安装Chrome:执行以下命令
      sudo apt update && sudo apt install -y google-chrome-stable
      
    2. 修改代码:删除chrome_options.binary_location = path这一行,或者替换为正确的Chrome浏览器路径:
      chrome_options.binary_location = "/usr/bin/google-chrome"
      
    3. 优化可选配置:--remote-debugging-port=9222在Celery多任务并发场景下容易出现端口冲突,建议移除该参数;start-maximized和window-size功能重复,保留一个即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 16:20:15