Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set proxy with authentication in selenium chrome web driver using python

I have following script to visit a web page using python selenium Chrome driver.

from selenium import webdriver
USERNAME = 'usename'
PASSWORD = 'pass'
proxies = ["xxx.xxx.xxx.xxx"]
proxy_tpl ='{0}:{1}'
proxy = proxy_tpl.format(proxies[0],'xx') 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://{0}:{1}@whatismyipaddress.com".format(USERNAME, PASSWORD))
driver.close()

Chrome still asking username and password when i try to run script. How can i authenticate proxy server from script ?

like image 227
open source guy Avatar asked May 01 '15 07:05

open source guy


People also ask

Which of the following code is used in Selenium to configure the use of proxy?

Following piece of code used to set proxy in Selenium. ChromeOptions option = new ChromeOptions(); Proxy proxy = new Proxy(); proxy. setHttpProxy("localhost:5555"); option. setCapability(CapabilityType.

What is a web proxy authentication?

The HTTP Proxy-Authenticate response header defines the authentication method that should be used to gain access to a resource behind a proxy server. It authenticates the request to the proxy server, allowing it to transmit the request further.


1 Answers

Inspired by this this solution in php, i wrote a equivalent in python:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import zipfile

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"
}
"""

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

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

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

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


pluginfile = 'proxy_auth_plugin.zip'

with zipfile.ZipFile(pluginfile, 'w') as zp:
    zp.writestr("manifest.json", manifest_json)
    zp.writestr("background.js", background_js)

co = Options()
co.add_argument("--start-maximized")
co.add_extension(pluginfile)


driver = webdriver.Chrome("path/to/chromedriver",  chrome_options=co)
driver.get("http://www.google.com.br")

In background_js string, replace the XXX with your information.

like image 96
user3286105 Avatar answered Sep 30 '22 10:09

user3286105