Selenium using Python: enter/provide http proxy password for firefox


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)

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.
7/5/2013 5:17:39 AM

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 containing two files:


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

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

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

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

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


    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
    "background": {
        "scripts": ["background.js"]
  1. Add the created as an extension

    from selenium import webdriver
    from import Options
    chrome_options = Options()
    driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)

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

3/8/2016 1:06:26 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow