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

Python Selenium启动Firefox报错:geckodriver意外退出(状态码-6)求解决

Selenium启动Firefox报错Status code was: -6的解决方法

问题详情

尝试运行以下Selenium代码启动Firefox:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://dev.to")

driver.find_element_by_id("nav-search").send_keys("Selenium")

运行后未启动Firefox,报错信息如下:

Traceback (most recent call last):
  File "/home/surf/Desktop/aq.py", line 3, in <module>
    driver = webdriver.Firefox()
  File "/home/surf/.local/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 172, in __init__
    self.service.start()
  File "/home/surf/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/home/surf/.local/lib/python3.10/site-packages/selenium/webdriver/common/service.py", line 110, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: -6

操作系统环境:Ubuntu 22.04.1 LTS(Linux Surf 5.15.0-48-generic #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux)

错误原因分析

Status code -6对应Linux系统的SIGABRT信号,表示进程异常终止,常见触发原因:

  • geckodriver与Firefox版本不兼容
  • geckodriver缺少执行权限
  • Firefox缺失必要的系统依赖库
  • 无图形界面环境下未启用headless模式
  • geckodriver本身损坏或安装路径不正确

解决方案

  • 检查版本兼容性:确认geckodriver版本与本地Firefox版本匹配,不同Firefox版本对应特定的geckodriver版本范围,确保两者版本适配。
  • 修复geckodriver权限:找到geckodriver所在路径,执行chmod +x /path/to/geckodriver赋予执行权限;同时确保该路径在系统PATH环境变量中,或初始化driver时指定geckodriver路径:
    driver = webdriver.Firefox(executable_path="/path/to/geckodriver")
    
  • 检查Firefox依赖:在终端运行ldd /usr/bin/firefox | grep not,查看是否有缺失的依赖库,根据提示安装对应的系统包(如libgtk-3-0、libdbus-glib-1-2等)。
  • 指定Firefox二进制路径:若系统存在多个Firefox实例,或默认路径不正确,初始化时显式指定Firefox路径:
    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.binary_location = "/usr/bin/firefox"  # 替换为实际Firefox路径
    driver = webdriver.Firefox(options=options)
    
  • 启用Headless模式(无GUI环境):如果在无桌面的服务器环境运行,添加headless参数:
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")  # 可选,解决权限限制
    
  • 重新安装geckodriver:下载对应Ubuntu x86_64版本的geckodriver,解压后移动到/usr/local/bin目录(全局可用),确保文件权限正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:55:25