如何使用Python + Selenium设置代理身份验证(用户和密码)

Raf*_*lah 10 firefox proxy-authentication python-2.7 selenium-webdriver

我在Python 2.7中使用Firefox WebDriver和Selenium.当我运行程序时,我的python程序启动Firefox浏览器并访问不同的网站.但是,我需要使用身份验证设置代理,这样当程序访问任何网站时,它将通过代理服务器访问.

SO上有一些类似的问题.但是,没有针对Selenium Firefox WebDriver的Python的特定解决方案.

Tim*_*ême 9

重复:如何使用 Python/Selenium 设置代理身份验证用户名:密码

硒线:https : //github.com/wkeeling/selenium-wire

安装硒线

pip install selenium-wire
Run Code Online (Sandbox Code Playgroud)

导入它

from seleniumwire import webdriver
Run Code Online (Sandbox Code Playgroud)

授权代理

options = {
'proxy': {
    'http': 'http://username:password@host:port',
    'https': 'https://username:password@host:port',
    'no_proxy': 'localhost,127.0.0.1,dev_server:8080'
    }
}
driver = webdriver.Firefox(seleniumwire_options=options)
Run Code Online (Sandbox Code Playgroud)

警告
查看 selenium-wire 缓存文件夹。我遇到了问题,因为它占用了我所有的磁盘空间。您有时必须在需要时在脚本中将其删除。

  • 只是对您的解决方案的补充。这仅支持 Python 3+.. (5认同)

Nun*_*dré 8

除了使用保存凭据的配置文件运行Firefox.你可以做到这一点装的是,在写的延伸loginTextboxpassword1Textboxchrome://global/content/commonDialog.xul(警报窗口).

已经有一些扩展可以完成这项工作.例如:Close Proxy Authentication

https://addons.mozilla.org/firefox/downloads/latest/close-proxy-authentication/addon-427702-latest.xpi

from selenium import webdriver
from base64 import b64encode

proxy = {'host': HOST, 'port': PORT, 'usr': USER, 'pwd': PASSWD}

fp = webdriver.FirefoxProfile()

fp.add_extension('closeproxy.xpi')
fp.set_preference('network.proxy.type', 1)
fp.set_preference('network.proxy.http', proxy['host'])
fp.set_preference('network.proxy.http_port', int(proxy['port']))
# ... ssl, socks, ftp ...
fp.set_preference('network.proxy.no_proxies_on', 'localhost, 127.0.0.1')

credentials = '{usr}:{pwd}'.format(**proxy)
credentials = b64encode(credentials.encode('ascii')).decode('utf-8')
fp.set_preference('extensions.closeproxyauth.authtoken', credentials)

driver = webdriver.Firefox(fp)
Run Code Online (Sandbox Code Playgroud)

  • 这个插件不再适用于新的firefox,任何其他想法我们能做什么? (3认同)
  • 链接不起作用 - 我猜你的意思是一个 -> https://addons.thunderbird.net/en-us/firefox/addon/close-proxy-authentication/ (似乎已过时) (3认同)

小智 5

您可以为代理编写自己的 Firefox 扩展,并从 selenium 启动。你需要写2个文件并打包。

背景.js

var proxy_host = "YOUR_PROXY_HOST";
var proxy_port = YOUR_PROXY_PORT;

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: proxy_host,
        port: proxy_port
      },
      bypassList: []
    }
 };


function proxyRequest(request_data) {
    return {
        type: "http",
        host: proxy_host, 
        port: proxy_port
    };
}

browser.proxy.settings.set({value: config, scope: "regular"}, function() {;});

function callbackFn(details) {
return {
    authCredentials: {
        username: "YOUR_USERNAME",
        password: "YOUR_PASSWORD"
    }
};
}

browser.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});
Run Code Online (Sandbox Code Playgroud)

清单.json

{
  "name": "My Firefox Proxy",
  "version": "1.0.0b",
  "manifest_version": 2,
  "permissions": [
    "browsingData",
    "proxy",
    "storage",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "downloads",
    "notifications",
    "<all_urls>"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_specific_settings": {
    "gecko": {
      "id": "myproxy@example.org"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

接下来,您需要将此文件打包为以 DEFLATED 模式压缩存档,末尾带有 .xpi ,如my_proxy_extension.xpi

你有两个选择:

  1. 签署您的扩展Firefox 扩展 .xpi 文件结构:描述、内容、创建和安装

或者

  1. 运行未签名。对于此步骤:

    1. 在 about:config 打开 firefox 标志并将选项xpinstall.signatures.required设置为false

      或者

    2. 更新 Firefox 配置文件:

      Windows:C:\Program Files\Mozilla Firefox\defaults\pref\channel-prefs.js

      Linux:/etc/firefox/syspref.js

    将下一行添加到文件末尾:

     pref("xpinstall.signatures.required",false);
    
    Run Code Online (Sandbox Code Playgroud)

执行此步骤后,运行 selenium 并安装此扩展:

from selenium import webdriver

driver = webdriver.Firefox()
driver.install_addon("path/to/my_proxy_extension.xpi")

driver.get("https://yoursite.com")


 
Run Code Online (Sandbox Code Playgroud)

  • 如果有人遇到问题,请在 background.js 中将“browser”替换为“chrome”。此代码使用回调,这就是 chrome 的做法。Firefox 使用承诺。如果您使用 chrome 关键字,它将在兼容模式下工作。 (2认同)

Eug*_*e S 0

这里有一个 Firefox + Python 但没有身份验证的示例。然后您可以在源代码中找到其他可用的参数。所以看起来您需要以下内容:

socksUsername
socksPassword
Run Code Online (Sandbox Code Playgroud)

例如:

from selenium import webdriver
from selenium.webdriver.common.proxy import *

myProxy = "host:8080"

proxy = Proxy({
    'proxyType': ProxyType.MANUAL,
    'httpProxy': myProxy, # set this value as desired
    'ftpProxy': myProxy,  # set this value as desired
    'sslProxy': myProxy,  # set this value as desired
    'noProxy': ''         # set this value as desired
    'socksUsername': = ''
    'socksPassword': = ''
    })

driver = webdriver.Firefox(proxy=proxy)
Run Code Online (Sandbox Code Playgroud)

或根据偏好:

driverPref = webdriver.FirefoxProfile()
driverPref.set_preference("network.proxy.type", 1)
.
.
.
driverPref.set_preference('network.proxy.socks', proxyHost)
driverPref.set_preference('network.proxy.socks_port', proxyPort)
driverPref.update_preferences()

driver = webdriver.Firefox(firefox_profile=driverPref)
Run Code Online (Sandbox Code Playgroud)

编辑

我又看了一遍,似乎无法在FF中设置身份验证详细信息,即使是手动设置也是如此。唯一的方法就是记住您已经输入的由 2 个参数完成的详细信息:

signon.autologin.proxy=true
network.websocket.enabled=false
Run Code Online (Sandbox Code Playgroud)

可以使用该set_preference()方法进行配置。您还可以通过浏览到 来手动查看所有 FF 选项about:config