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

CloudLinux x86_64/Cpanel环境下Selenium-WebDriver运行异常求助

ChromeDriver在CloudLinux服务器意外退出(状态码-5)排查与解决

环境差异

  • 本地:Windows 10 + Python 3.12,代码运行正常
  • 服务器:CloudLinux x86_64/Cpanel + Python 3.11.5,触发Selenium异常

核心错误堆栈

Service /home/project/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver unexpectedly exited. Status code was: -5
Traceback (most recent call last):
  File "/home/project/repositories/fam/fam.py", line 23, in init_browser
    browser = webdriver.Chrome(options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__
    super().__init__(
  File "/home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 53, in __init__
    self.service.start()
  File "/home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 102, in start
    self.assert_process_still_running()
  File "/home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/common/service.py", line 115, in assert_process_still_running
    raise WebDriverException(f"Service {self._path} unexpectedly exited. Status code was: {return_code}")
selenium.common.exceptions.WebDriverException: Message: Service /home/project/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver unexpectedly exited. Status code was: -5

浏览器初始化代码

def init_browser():
    # 初始化浏览器
    logging.info("Initializing browser object")
    try:
        options = webdriver.ChromeOptions()
        options.add_argument('--headless=new') 
        # 阻止图片加载
        options.experimental_options['prefs'] = {
        "profile.managed_default_content_settings.images": 2
        }
        browser = webdriver.Chrome(options=options)
    except Exception as ex:
        logging.exception(ex)
        sys.exit()
    else:
        return browser

Selenium Manager日志

Selenium Manager binary found at: /home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/common/linux/selenium-manager
Executing process: /home/project/virtualenv/repositories/fam/3.11/lib/python3.11/site-packages/selenium/webdriver/common/linux/selenium-manager --browser chrome --debug --output json
chromedriver not found in PATH
chrome not found in PATH
chrome not found in the system
Discovering versions from https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json
Required browser: chrome 119.0.6045.105
chrome 119.0.6045.105 already exists
chrome 119.0.6045.105 is available at /home/project/.cache/selenium/chrome/linux64/119.0.6045.105/chrome
Discovering versions from https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
Required driver: chromedriver 119.0.6045.105
chromedriver 119.0.6045.105 already in the cache
Driver path: /home/project/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver
Browser path: /home/project/.cache/selenium/chrome/linux64/119.0.6045.105/chrome
Using driver at: /home/qproject/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver
Started executable: `/home/project/.cache/selenium/chromedriver/linux64/119.0.6045.105/chromedriver` in a child process with pid: 365909

已尝试的无效方案

  • 使用本地下载的webdriver和浏览器可执行文件
  • 切换Chrome/Firefox浏览器测试
  • 配置自定义调试端口(如--remote-debugging-port=8080)
  • 添加参数--disable-dev-shm-usage、--no-sandbox
  • 组合上述所有方案

服务器文件权限已排查无异常。

使用自定义Chrome二进制文件的新报错

2023-12-01 13:19:23 - INFO - root - Initializing browser object
2023-12-01 13:19:23 - INFO - root - Browser type is chrome
2023-12-01 13:19:23 - INFO - root - Local files are used
2023-12-01 13:19:23 - DEBUG - selenium.webdriver.common.service - Started executable: `/home/project/home/selenium_drivers/chromedriver-linux64/chromedriver` in a child process with pid: 1431566
2023-12-01 13:19:24 - ERROR - root - Message: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /home/project/home/selenium_bin/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

可行解决建议

  1. 明确指定Chrome二进制路径
    在ChromeOptions中手动指定浏览器可执行文件位置,避免Selenium Manager自动下载的文件出现兼容性问题:

    options.binary_location = "/home/project/home/selenium_bin/chrome"
    
  2. 补充无头模式必备参数
    CloudLinux这类受限环境需要更完整的无头配置,添加以下参数:

    options.add_argument('--disable-gpu')
    options.add_argument('--window-size=1920x1080')
    options.add_argument('--single-process')
    options.add_argument('--no-zygote')
    
  3. 检查并补充Chrome依赖库
    执行命令检查Chrome缺失的系统依赖:

    ldd /home/project/.cache/selenium/chrome/linux64/119.0.6045.105/chrome | grep not
    

    根据输出安装对应依赖包(如glibc-devel、libnss3、libgbm等)。

  4. 用Xvfb模拟图形环境
    服务器无图形界面时,安装Xvfb并启动虚拟显示:

    Xvfb :99 -screen 0 1920x1080x24 &
    export DISPLAY=:99
    

    也可以用pyvirtualdisplay库在代码中自动管理:

    from pyvirtualdisplay import Display
    display = Display(visible=0, size=(1920, 1080))
    display.start()
    
  5. 降级Chrome版本
    尝试切换到更稳定的旧版本Chrome(如118.x系列),新版本可能与CloudLinux环境存在兼容性冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:39:53