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

使用Selenium+WebDriverManager时chromedriver意外退出报错求助

解决Selenium启动ChromeDriver时报错Status code was: -6的问题

问题概述

使用以下环境配置启动ChromeDriver时出现错误:

  • Python 3.7.13
  • Selenium 4.4.3
  • webdrivermanager 0.10.0

运行代码:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

报错信息:

WebDriverException                        Traceback (most recent call last)
<ipython-input-30-470b559d9ec4> in <module>
      2 from selenium import webdriver
      3 from webdriver_manager.chrome import ChromeDriverManager
----> 4 driver = webdriver.Chrome(ChromeDriverManager().install())
      5 import pandas as pd
      6 import time

3 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
    110             raise WebDriverException(
    111                 'Service %s unexpectedly exited. Status code was: %s'
---> 112                 % (self.path, return_code)
    113             )
    114 
WebDriverException: Message: Service /root/.wdm/drivers/chromedriver/linux64/104.0.5112/chromedriver unexpectedly exited. Status code was: -6

解决方案

错误码-6通常由版本不匹配、系统依赖缺失、权限或无桌面环境限制导致,可尝试以下方案:

1. 确保Chrome与ChromeDriver版本严格匹配

  • 打开Chrome浏览器,在地址栏输入chrome://version/查看当前Chrome版本(例如104.0.5112.79)
  • 修改代码手动指定对应版本的ChromeDriver:
    from selenium import webdriver
    from webdriver_manager.chrome import ChromeDriverManager
    
    # 将driver_version替换为你的Chrome主版本对应的完整版本号
    driver = webdriver.Chrome(ChromeDriverManager(driver_version="104.0.5112.79").install())
    

2. 安装Linux系统缺失的依赖库

由于报错路径显示为Linux环境,运行以下命令补充依赖:

  • Debian/Ubuntu系:
    sudo apt-get update && sudo apt-get install -y libnss3 libgconf-2-4 libxss1 libappindicator1 libindicator7
    
  • CentOS/RHEL系:
    sudo yum install -y nss libXScrnSaver
    

3. 启用无头模式并添加权限参数(服务器无桌面环境时)

如果在无GUI的服务器上运行,需添加无头模式及权限相关配置:

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

chrome_options = Options()
chrome_options.add_argument("--headless")  # 无头模式
chrome_options.add_argument("--no-sandbox")  # 绕过沙箱限制,解决权限问题
chrome_options.add_argument("--disable-dev-shm-usage")  # 避免/dev/shm空间不足
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

4. 修复ChromeDriver可执行权限

给下载的ChromeDriver添加执行权限:

chmod +x /root/.wdm/drivers/chromedriver/linux64/104.0.5112/chromedriver

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 06:22:05