Inject Proxy Settings in Selenium/Chrome
1 min read

Inject Proxy Settings in Selenium/Chrome

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


The manifest file is:

  "version": "1.0.0",
  "manifest_version": 2,
  "name": "Chrome Proxy",
  "permissions": [
  "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: [""]

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

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

            {urls: ["<all_urls>"]},

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


The plugin is created by zipping the two files:

pluginfile = ''

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


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"

driver = webdriver.Chrome(chrome_options=co)

The full script is in the Gist below: