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

如何配置Ruby Selenium忽略不受信任证书(RSpec+Capybara环境)

解决Ruby RSpec+Selenium+Capybara访问HTTPS站点的不受信任证书问题

我来帮你搞定这个证书验证报错的问题!你已经找对方向了,就是通过配置浏览器profile/选项来跳过不受信任证书的验证,下面给你补全正确的代码和不同浏览器的方案:

Firefox浏览器配置

你之前的代码已经写了核心的配置项,只需要补全完整的driver注册代码即可:

Capybara.register_driver :selenium do |app|
  # 创建Firefox配置文件实例
  profile = Selenium::WebDriver::Firefox::Profile.new
  # 设置忽略不受信任的证书颁发机构
  profile.assume_untrusted_certificate_issuer = false
  # 初始化driver并传入配置好的profile
  Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end

Chrome浏览器配置(可选)

如果后续你切换到Chrome浏览器,配置方式略有不同,需要设置对应的选项来忽略证书错误:

Capybara.register_driver :selenium_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  # 方式1:通过命令行参数忽略证书错误
  options.add_argument('--ignore-certificate-errors')
  # 方式2:使用Selenium官方推荐的选项(更标准)
  options.accept_insecure_certs = true
  
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

额外提示

  • 配置完driver后,记得在你的spec配置中指定使用这个driver:
    Capybara.default_driver = :selenium # 对应Firefox配置
    # 或者 Capybara.default_driver = :selenium_chrome # 对应Chrome配置
    
  • assume_untrusted_certificate_issuer = false的作用是让Firefox信任所有证书颁发机构,从而跳过证书验证环节;Chrome的两种配置方式效果一致,任选其一即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:31:05