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

如何解决Selenium中Firefox驱动二进制文件的配置问题

问题原因与解决方案

你搞混了两个关键路径的配置:

  • options.binary_location 是用来指定Firefox浏览器的可执行文件路径,而非geckodriver驱动文件
  • Service(executable_path) 才是指定geckodriver驱动文件的路径

你的代码里把geckodriver同时填给了这两个参数,导致Selenium误以为geckodriver是Firefox浏览器程序,因此抛出"binary is not a Firefox executable"错误。


修正步骤

  1. 定位Firefox浏览器的实际路径

    • Windows:若为自定义目录,填写你解压后的firefox.exe路径,比如C:\custom-firefox\firefox.exe
    • Linux:填写解压后的firefox可执行文件路径,比如/home/your-name/my-firefox/firefox
    • Mac:路径通常为/Applications/Firefox.app/Contents/MacOS/firefox
  2. 修正代码配置
    将options.binary_location替换为Firefox浏览器的路径,Service的executable_path保留geckodriver的自定义路径(若不在当前工作目录,务必填写绝对路径):

from selenium import webdriver
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.firefox.service import Service

# 配置Firefox浏览器路径
options = FirefoxOptions()
# 替换成你实际的Firefox可执行文件路径,以下为Linux示例
options.binary_location = r'/home/your-name/custom-dir/firefox'
# Windows示例:options.binary_location = r'C:\custom-dir\firefox.exe'
# Mac示例:options.binary_location = r'/Applications/Firefox.app/Contents/MacOS/firefox'

# 配置geckodriver驱动路径
s = Service(executable_path=r'/home/your-name/custom-dir/geckodriver')
driver = webdriver.Firefox(service=s, options=options)

额外注意事项

  • 若geckodriver不在当前工作目录,必须使用绝对路径,否则Selenium无法找到该文件
  • Linux/Mac系统下,需给geckodriver添加可执行权限:执行chmod +x /path/to/geckodriver
  • 确保geckodriver版本与Firefox版本匹配,版本不兼容也会导致启动失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:39:54