I have the following situation: I'm behind a corporate proxy and I need to run a test accessing an external website. To do this, I've picked up chrome/chromium as driver of choice. My current solution implies an authentication via plugin.

The plugin

The chrome plugin is essentially 2 files:

  • a manifest file
  • a JS containing a listener

Manifest

The manifest file is:

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

The script

Following script performs the proxy authentication:

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

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

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%(user)s",
            password: "%(pass)s"
        }
    };
}

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

,where the PROXY_* variables are the components of the proxy URL.

Plugin

The plugin is created by zipping the two files:

pluginfile = 'proxy_auth_plugin.zip'

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

Selenium

Once the zip file is created, we need to add it to the chrome:

co = Options()
co.binary_location = "C:\\PGM\\bin\\chrome-win32\\chrome.exe"
co.add_extension(pluginfile)


driver = webdriver.Chrome(chrome_options=co)
driver.get("http://www.google.com")

The full script is in the Gist below:

HTH,