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

如何在Firefox的Watir与Selenium WebDriver中使用带账号密码的私有代理

使用Watir + Selenium配置带用户名密码的Firefox私有代理

嘿,我来帮你搞定带认证的私有代理配置!你当前的代码已经搭好了基础的代理设置,但要添加用户名密码认证的话,得注意:Firefox现在不支持直接通过about:config的preferences来设置代理账号密码,所以得换两种更靠谱的方式——优先推荐用自定义扩展(稳定无弹窗),备选是处理原生认证弹窗(局限性大)。

方法一:自定义Firefox扩展(推荐,无弹窗自动认证)

这种方式是目前最稳定的,因为扩展可以直接接管代理配置并注入认证信息,不会触发烦人的浏览器弹窗。步骤如下:

  1. 先确保你安装了rubyzip gem(用来打包临时扩展),没装的话先跑:
gem install rubyzip
  1. 修改你的配置代码,添加扩展生成逻辑:
require 'zip'
require 'tempfile'

# 1. 定义Firefox扩展的manifest和后台脚本
extension_manifest = <<~JSON
{
  "manifest_version": 2,
  "name": "Proxy Auth AutoHandler",
  "version": "1.0",
  "permissions": ["proxy", "tabs", "<all_urls>", "webRequest", "webRequestBlocking"],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_specific_settings": {
    "gecko": {
      "id": "proxy-auth-handler@yourdomain.com"
    }
  }
}
JSON

background_script = <<~JS
// 接管代理设置并注入认证信息
browser.proxy.onRequest.addListener((details) => {
  return {
    type: "http",
    host: "#{ENV['PROXY_IP']}",
    port: #{ENV['PROXY_PORT'].to_i},
    proxyAuthorization: "Basic " + btoa("#{ENV['PROXY_USERNAME']}:#{ENV['PROXY_PASSWORD']}")
  };
}, {urls: ["<all_urls>"]});

// 备用:处理可能漏网的认证弹窗
browser.webRequest.onAuthRequired.addListener((details) => {
  return {
    authCredentials: {
      username: "#{ENV['PROXY_USERNAME']}",
      password: "#{ENV['PROXY_PASSWORD']}"
    }
  };
}, {urls: ["<all_urls>"]}, ["blocking"]);
JS

# 2. 生成临时扩展zip包
temp_extension = Tempfile.new(['proxy_auth_ext', '.zip'])
Zip::File.open(temp_extension.path, Zip::File::CREATE) do |zip|
  zip.get_output_stream('manifest.json') { |f| f.write(extension_manifest) }
  zip.get_output_stream('background.js') { |f| f.write(background_script) }
end
temp_extension.close

# 3. 配置Firefox并添加扩展
profile = Selenium::WebDriver::Firefox::Profile.new
# 这里不需要再设置network.proxy的preferences,扩展会完全接管
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
options.add_extension(temp_extension.path)

# 初始化driver
driver = Selenium::WebDriver.for :firefox, options: options

这个扩展会自动帮你处理所有请求的代理认证,不管是HTTP还是HTTPS,全程不会有弹窗,兼容性也最好。

方法二:处理原生代理认证弹窗(备选,局限性大)

如果只是临时测试,也可以尝试处理浏览器弹出的原生HTTP认证窗口,但要注意:只有当代理使用的是标准HTTP Basic认证时才有效,如果是网页形式的认证,这个方法就没用了。

修改你的原有代码,添加弹窗处理逻辑:

# 保持你原来的代理profile配置
profile = Selenium::WebDriver::Firefox::Profile.new
profile["network.proxy.type"] = 1
profile["network.proxy.http"] = ENV['PROXY_IP']
profile["network.proxy.ssl"] = ENV['PROXY_IP']
profile["network.proxy.http_port"] = ENV['PROXY_PORT'].to_i
profile["network.proxy.ssl_port"] = ENV['PROXY_PORT'].to_i

options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
driver = Selenium::WebDriver.for :firefox, options: options

# 尝试处理认证弹窗(注意:必须是浏览器原生弹窗,不是网页弹窗)
begin
  alert = driver.switch_to.alert
  # 用Tab键分隔用户名和密码
  alert.send_keys("#{ENV['PROXY_USERNAME']}\t#{ENV['PROXY_PASSWORD']}")
  alert.accept
rescue Selenium::WebDriver::Error::NoAlertPresentError
  # 没有弹窗的话,说明代理不需要认证或者已经自动处理了,忽略即可
end

这种方法的问题在于:有时候浏览器会拦截弹窗,或者代理的认证不是原生弹窗,导致代码失效,所以只适合临时调试用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:08:13