使用Python的Selenium:输入/提供Firefox的http代理密码 [英] Selenium using Python: enter/provide http proxy password for firefox

查看:181
本文介绍了使用Python的Selenium:输入/提供Firefox的http代理密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个密码保护的代理来使用硒。代理不是固定的,而是一个变量。所以这个必须在代码中完成(在这个特定的机器上设置firefox来处理代理是不太理想的)。到目前为止,我有下面的代码:

$ $ p $ web $ $ $ $ $ $ b $直接= 0,手动= 1,PAC = 2,AUTODETECT = 4,SYSTEM = 5
fp.set_preference(network.proxy.type,1)

fp.set_preference(network.proxy.http ,PROXY_HOST)
fp.set_preference(network.proxy.http_port,PROXY_PORT)

driver = webdriver.Firefox(firefox_profile = fp)
driver.get(http: //whatismyip.com)

此时弹出对话框,请求代理用户/ 。



有没有简单的方法可以:


  1. 输入用户名/传递给对话框。
  2. 在早期阶段提供用户/传递。


解决方案

硒本身不能这样做。我发现有用的唯一方法是此处。简而言之,您需要添加一个浏览器扩展,以进行身份​​验证。这是它看起来很简单。



以下是Chrome的工作原理(在我的情况下):


  1. 创建包含两个文件的zip文件 proxy.zip

background.js

  var config = {
mode:fixed_servers,
规则:{
singleProxy:{
scheme:http,
host:YOU_PROXY_ADDRESS,
port:parseInt(YOUR_PROXY_PORT)
},
bypassList:[foobar.com]
}
};

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

函数callbackFn(details){
return {
authCredentials:{
用户名:YOUR_PROXY_USERNAME,
密码:YOUR_PROXY_PASSWORD
}
};


chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{url:[< all_urls>]},
[阻止']
);

不要忘记将 YOUR_PROXY _ * 替换为您的设置。



manifest.json
$ b

  {
version:1.0.0,
manifest_version:2,
name:Chrome代理,
permissions:[
proxy ,
tabs,
unlimitedStorage,
storage,
all_urls>,
webRequest,
webRequestBlocking

background:{
scripts:[background.js]
},
minimum_chrome_version:22.0.0




  1. 将创建的proxy.zip添加为扩展名

      from selenium import webdriver $ b $ from selenium.webdriver.chrome.options import选项

    chrome_options =选项()
    chrome_options.add_extension(proxy.zip)

    驱动程序= webdriver.Chrome(executable_path ='c hromedriver.exe',chrome_options = chrome_options)
    driver.get(http://google.com)
    driver.close()


就是这样。对我来说,就像一个魅力。如果您需要动态创建proxy.zip或需要PHP示例,请转到原始帖子


I want to use selenium with a proxy which is password protected. The proxy is not fixed, but a variable. So this has to be done in the code (just setting up firefox on this particular machine to work with the proxy is less-than-ideal). So far I have the following code:

fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
fp.set_preference("network.proxy.type", 1)

fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", PROXY_PORT)

driver = webdriver.Firefox(firefox_profile=fp)
driver.get("http://whatismyip.com")

At this point, the dialog pops up requesting the proxy user/pass.

Is there an easy way to either:

  1. Type in the user/pass in the dialog box.
  2. Provide the user/pass at an earlier stage.

解决方案

Selenium can't do that by itself. The only way I found helpful is described here. To be short, you need to add a browser extension on fly that does the authentication. It's much simpler than may seem to be.

Here is how it works for Chrome (in my case):

  1. Create a zip file proxy.zip containing two files:

background.js

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "YOU_PROXY_ADDRESS",
        port: parseInt(YOUR_PROXY_PORT)
      },
      bypassList: ["foobar.com"]
    }
  };

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

function callbackFn(details) {
    return {
        authCredentials: {
            username: "YOUR_PROXY_USERNAME",
            password: "YOUR_PROXY_PASSWORD"
        }
    };
}

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

Don't forget to replace YOUR_PROXY_* to your settings.

manifest.json

{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}

  1. Add the created proxy.zip as an extension

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_extension("proxy.zip")
    
    driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
    driver.get("http://google.com")
    driver.close()
    

That's it. For me that worked like a charm. If you need to create proxy.zip dynamically or need PHP example then go to the original post

这篇关于使用Python的Selenium:输入/提供Firefox的http代理密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆