Question

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:pass@192.168.1.1: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.

Was it helpful?

Solution

What you can do is to create a profile and save the authentication data in it. If your profile is called "webdriver" you can select it from your code in the initialization:

ProfilesIni allProfiles = new ProfilesIni(); 
FirefoxProfile profile = allProfiles.getProfile("WebDriver"); 
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);

OTHER TIPS

        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))

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": "myproxy@example.org"
    }
  }
}

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);

Did it with MS UI Automation without AutoIt:

public void AuthInProxyWindow (string login, string pass)
    {
        var proxyWindow = AutomationElement.RootElement
            .FindFirst(TreeScope.Subtree,
                new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass"));

        var edits = proxyWindow.FindAll(TreeScope.Subtree,
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));

        var unamePoint = edits[1].GetClickablePoint();
        Mouse.MoveTo(new Point((int) unamePoint.X, (int) unamePoint.Y));
        Mouse.Click(MouseButton.Left);

        SendKeys.SendWait(login);
        var pwdPoint = edits[2].GetClickablePoint();
        Mouse.MoveTo(new Point((int) pwdPoint.X, (int) pwdPoint.Y));
        Mouse.Click(MouseButton.Left);
        SendKeys.SendWait(pass);

        Keyboard.Press(Key.Return);
        Logger.Debug("Authefication in Firefox completed succesfully");
    }

Mouse moves by Microsoft.TestApi

To stop firefox from giving you the auth pop up simple make sure you set your proxy URL to include the auth details in the setup stage as below:

var myProxy = user + ":" + pass + "@" + proxyIP + ":" + proxyPORT;
options.SetPreference("network.proxy.type", 1);
options.SetPreference("network.proxy.http", myProxy);
options.SetPreference("network.proxy.http_port", proxyPORT);
options.SetPreference("general.useragent.override", useragent);
driver = new FirefoxDriver(driverService, options);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top