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

问题描述 投票:0回答:3

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

SO上也有一些类似的问题。但是,Python 的 Selenium Firefox WebDriver 没有具体的解决方案。

python python-2.7 selenium-webdriver firefox proxy-authentication
3个回答
11
投票

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

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

安装硒线

pip install selenium-wire

导入

from seleniumwire import webdriver

授权代理

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)

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


8
投票

除了使用已保存凭据的配置文件运行 Firefox 之外。您可以加载一个在

loginTextbox
password1Textbox
chrome://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)

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>"]});

manifest.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": "[email protected]"
    }
  }
}

接下来,您需要将此文件打包到 DEFLATED 模式下的 zip 存档中,末尾带有 .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\pre

© www.soinside.com 2019 - 2024. All rights reserved.