18

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

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

4

6 回答 6

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 缓存文件夹。我遇到了问题,因为它占用了我所有的磁盘空间。有时,您必须在需要时在脚本中将其删除。

于 2019-12-11T14:38:57.623 回答
8

除了使用已保存凭据的配置文件运行 Firefox。您可以加载一个写入loginTextboxpassword1Textboxof 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)
于 2016-10-06T18:52:34.560 回答
2

您可以为代理编写自己的 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>"]});

清单.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"
    }
  }
}

接下来,您需要将这些文件打包到 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);
    

在此步骤之后运行 selenium 并安装此扩展:

from selenium import webdriver

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

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


 
于 2021-01-22T16:35:06.680 回答
0

除了带有扩展名的答案。

您还可以使用表单填写来动态更改代理上的凭据。只需加载扩展页面,自动填写表格并点击保存!

于 2017-06-07T05:16:36.987 回答
0

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

socksUsername
socksPassword

例如:

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)

或有偏好:

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)

编辑

我又看了一遍,似乎在FF中设置认证细节是不可能的,即使是手动的。唯一的方法就是记住您已经输入的由 2 个参数完成的详细信息:

signon.autologin.proxy=true
network.websocket.enabled=false

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

于 2016-07-11T13:31:09.033 回答
0

我们可以切换到认证提示框,手动输入用户名密码。

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference('network.proxy.ssl_port', int(ProxyPort))
profile.set_preference('network.proxy.ssl', ProxyHost)
profile.set_preference("network.proxy.http", ProxyHost)
profile.set_preference("network.proxy.http_port", int(ProxyPort))

webdriver = webdriver.Firefox(firefox_profile=profile, executable_path=GECKO_DRIVER_PATH)

webdriver.get("https://whatismyipaddress.com/")
try:
    alert = webdriver.switch_to_alert()
    print("switched to alert window")
    alert.send_keys(proxy_username + Keys.TAB + proxy_password)
    alert.accept()
    webdriver.switch_to.default_content()
except Exception:
    print("Error in alert switch")
    pass
于 2022-01-24T19:23:31.223 回答