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

Capybara+Selenium+Firefox+Rails自动保存文件失效问题求助

解决Firefox+Capybara+Selenium自动下载配置不生效的问题

我之前也踩过Firefox自动下载配置的坑,结合你的代码来看,几个调整点应该能解决问题:

1. 确保下载路径是绝对路径

Firefox无法直接解析~/Downloads这种tilde路径,得用Ruby的File.expand_path转换成系统能识别的绝对路径:

profile['browser.download.dir'] = File.expand_path("~/Downloads")

2. 补充标准PDF MIME类型

很多现代网站的PDF使用标准的application/pdf MIME类型,你的配置里没包含这个,这会导致Firefox还是弹出保存提示:

profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf"
profile['browser.helperApps.neverAsk.openFile'] = "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf"

3. 适配Selenium/Firefox版本的配置方式

如果你的Selenium和Geckodriver版本较新,直接传递Profile可能有兼容性问题,可以尝试用Options的add_preference方法来设置,替代直接操作Profile对象:

options = Selenium::WebDriver::Firefox::Options.new
options.add_preference('browser.download.folderList', 2)
options.add_preference('browser.download.dir', File.expand_path("~/Downloads"))
options.add_preference('browser.helperApps.alwaysAsk.force', false)
options.add_preference('browser.download.manager.showWhenStarting', false)
options.add_preference('browser.helperApps.neverAsk.openFile', "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
options.add_preference('browser.helperApps.neverAsk.saveToDisk', "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
options.add_preference('pdfjs.disabled', true)
options.add_preference('browser.download.useDownloadDir', true)
options.add_preference('plugin.scan.plid.all', false)
options.add_preference('plugin.scan.Acrobat', '99.0')
# 注释掉native_events,新版本Firefox已不再支持该配置
# options.add_preference('native_events', true)

4. 调试技巧

如果还是不生效,可以开启Geckodriver的调试日志,查看下载过程中的错误信息:

options.log_level = :debug
# 或者指定日志文件
options.log_path = File.expand_path("~/geckodriver.log")

修改后的完整配置代码:

Capybara.register_driver :selenium do |app|
  options = Selenium::WebDriver::Firefox::Options.new
  options.add_preference('browser.download.folderList', 2)
  options.add_preference('browser.download.dir', File.expand_path("~/Downloads"))
  options.add_preference('browser.helperApps.alwaysAsk.force', false)
  options.add_preference('browser.download.manager.showWhenStarting', false)
  options.add_preference('browser.helperApps.neverAsk.openFile', "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
  options.add_preference('browser.helperApps.neverAsk.saveToDisk', "application/pdf, application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
  options.add_preference('pdfjs.disabled', true)
  options.add_preference('browser.download.useDownloadDir', true)
  options.add_preference('plugin.scan.plid.all', false)
  options.add_preference('plugin.scan.Acrobat', '99.0')

  Capybara::Selenium::Driver.new(app, browser: :firefox, options: options)
end

Capybara.javascript_driver = :firefox
Capybara.configure do |config|
  config.default_max_wait_time = 1000000
  config.default_driver = :selenium
end

另外,记得确认你的Geckodriver版本和Firefox版本是兼容的,版本不匹配也可能导致配置不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:52:52