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

Selenium使用Chromedriver无头模式运行失败求助

解决无头模式下ChromeDriver提示Chrome崩溃的问题

针对你在VirtualBox Ubuntu 22.04环境下,无头模式运行Selenium脚本时出现的Chrome崩溃问题,可尝试以下几种解决方法:

1. 切换到新版无头模式并指定窗口大小

旧版--headless参数的行为和正常Chrome差异较大,容易触发崩溃,改用新版无头模式并明确窗口大小:

  • 替换--headless为--headless=new(Chrome 112+支持,兼容性更接近正常Chrome)
  • 移除--start-maximized,改用--window-size=1920,1080(无头模式下最大化参数不生效)

调整后的代码:

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

chrome_driver_binary = '/home/user/Projects/myproject/chromedriver'
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--no-sandbox") 
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path=chrome_driver_binary, options=options)
driver.get('http://www.ubuntu.com/')

2. 验证Chrome与ChromeDriver版本完全匹配

版本不兼容是无头模式崩溃的常见原因:

  • 终端运行google-chrome --version查看Chrome版本
  • 运行/home/user/Projects/myproject/chromedriver --version查看驱动版本
  • 两者版本号必须完全一致(如均为118.0.5993.70),不一致则下载对应版本的ChromeDriver替换

3. 确保chromedriver有执行权限

如果驱动文件没有可执行权限,也会导致启动失败:

chmod +x /home/user/Projects/myproject/chromedriver

4. 更新虚拟环境中的Selenium

旧版本Selenium对新版无头模式支持不佳,执行升级命令:

pip install --upgrade selenium

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 09:23:33