GitHub Actions中Selenium突然失效,启动WebDriver时报错Process closed with status 1
解决GitHub Actions中Selenium驱动启动失败(Process unexpectedly closed with status 1)的方案
这种报错大概率是GitHub Actions runner环境的浏览器/驱动版本不匹配,或者无头模式缺少依赖导致的,以下是针对性的解决步骤:
强制匹配浏览器与驱动版本
避免runner自动更新浏览器导致的兼容性问题,用webdriver-manager自动安装对应版本的驱动,同时启用Chrome新无头模式(老headless模式在新浏览器中兼容性差):from selenium import webdriver from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager options = Options() options.add_argument("--headless=new") options.add_argument("--no-sandbox") # 解决runner的权限限制 options.add_argument("--disable-dev-shm-usage") # 缓解runner内存不足问题 driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options) driver.get("https://hplanning2023.umons.ac.be/invite")也可以在GitHub Actions workflow中固定Chrome版本,避免自动更新:
- name: 固定Chrome版本 uses: browser-actions/setup-chrome@v1 with: chrome-version: 118.0.5993.70补充浏览器运行依赖
Ubuntu runner默认缺少部分浏览器运行所需的系统库,在workflow中添加安装步骤:- name: 安装系统依赖 run: | sudo apt-get update sudo apt-get install -y libnss3 libgconf-2-4 libxss1 libappindicator1 libindicator7升级Selenium到兼容版本
旧版Selenium与新版浏览器存在兼容性问题,升级到稳定版:pip install --upgrade selenium>=4.10.0调试排查具体错误
在workflow中添加日志输出,查看浏览器启动时的详细错误:- 在代码中添加
import logging; logging.basicConfig(level=logging.DEBUG) - 或者在启动驱动时添加
options.add_argument("--verbose"),输出详细启动日志
- 在代码中添加
内容的提问来源于stack exchange,提问作者Florian Wauters
相关产品推荐
相关产品推荐

