Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Selenium WebDriver FireFox Profile - using proxy with Authentication

When you set proxy server parameter in the code below if your proxy server requires authentication then FireFox will bring Authentication dialog and basically you can't fill it in automatically. So is there is anyway to set USERNAME and PASSWORD ?

FirefoxProfile profile = new FirefoxProfile();
String PROXY = "192.168.1.100:8080";
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;
profile.SetProxyPreferences(proxy);
FirefoxDriver driver = new FirefoxDriver(profile);

If you try to format proxy string to something like that http://username:[email protected]:8080 You get error that string is invalid. So I wonder there is must be a way of achieving this.

Any help would be appreciated.

like image 688
Tim Avatar asked Aug 29 '12 21:08

Tim


2 Answers

        String PROXY = "http://login:pass@proxy:port";
        ChromeOptions options = new ChromeOptions();

        options.AddArguments("user-data-dir=path/in/your/system");

        Proxy proxy = new Proxy();

        proxy.HttpProxy = PROXY;
        proxy.SslProxy  = PROXY;
        proxy.FtpProxy  = PROXY;

        options.Proxy = proxy;

        // Initialize the Chrome Driver
        using (var driver = new ChromeDriver(options))
like image 184
DevAnimal Avatar answered Sep 19 '22 07:09

DevAnimal


You can write own firefox extension for proxy, and launch from selenium. You need write 2 files and pack it.

background.js

var proxy_host = "YOUR_PROXY_HOST";
var proxy_port = YOUR_PROXY_PORT;

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: proxy_host,
        port: proxy_port
      },
      bypassList: []
    }
 };


function proxyRequest(request_data) {
    return {
        type: "http",
        host: proxy_host, 
        port: proxy_port
    };
}

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

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

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

browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});

manifest.json

{
  "name": "My Firefox Proxy",
  "version": "1.0.0b",
  "manifest_version": 2,
  "permissions": [
    "browsingData",
    "proxy",
    "storage",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "downloads",
    "notifications",
    "<all_urls>"
  ],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_specific_settings": {
    "gecko": {
      "id": "[email protected]"
    }
  }
}

Next you need packed this files to zip archive in DEFLATED mode with .xpi at end like my_proxy_extension.xpi.

You have two choices:

  1. Sign your extension Here you can read more about verify extension and extension's structure

    OR

  2. Run unsigned. For this step:

    • Open firefox flags at about:config and set options xpinstall.signatures.required to false

    OR

    • Update firefox profile in:

      Windows: C:\Program Files\Mozilla Firefox\defaults\pref\channel-prefs.js

      Linux: /etc/firefox/syspref.js

    Add next line to end of file:

    pref("xpinstall.signatures.required",false);

After this steps run selenium and install this extension:

FirefoxProfile profile = new FirefoxProfile();

profile.addExtension(new File("path/to/my_proxy_extension.xpi"));

driver = new FirefoxDriver(profile);
like image 22
Feuermann Avatar answered Sep 19 '22 07:09

Feuermann