2

I use Selenium WebDriver in my C# winforms application. i have a problem in aoutologin. After opening Firefox pop up Alert with input fields username and password.

 var profile = new FirefoxProfile();
 profile.SetPreference("general.useragent.override", [UserAgent]);

                Proxy proxy = new Proxy();
                proxy.HttpProxy = proxy;
                proxy.FtpProxy = proxy;
                proxy.SslProxy = proxy;
                proxy.SocksProxy = proxy;
                proxy.SocksUserName = username;
                proxy.SocksPassword = password;
                profile.SetProxyPreferences(proxy);
                profile.SetPreference("network.websocket.enabled", false);                    

                IWebDriver driver = new FirefoxDriver(profile);
                driver.Url = siteUrl;
hsayadyan
  • 21
  • 1
  • 3

4 Answers4

1

This is an old thread, but this is how I made it to work:

  public static bool InitializeAndSetupBrowser(string proxyIp, string proxyUsername, string proxyPassword, string proxyPort)
    {
    try
    {
        var proxy = new
        {
            Ip = proxyIp,
            Username = proxyUsername,
            Password = proxyPassword,
            Port = proxyPort
        };

        string PROXY = proxy.Ip + ":" + proxy.Port;

        Proxy pro = new Proxy();
        pro.HttpProxy = PROXY;
        pro.FtpProxy = PROXY;
        pro.SslProxy = PROXY;

        FirefoxOptions firefoxOptions = new FirefoxOptions();
        firefoxOptions.Proxy = pro;

        PropertiesCollection.Driver = new FirefoxDriver(firefoxOptions);
        Navigate(""); //this method is my internal method, just navigate in to page, this makes the proxy credentials dialog to appear
        try
        {

            WebDriverWait wait = new WebDriverWait(PropertiesCollection.Driver, TimeSpan.FromSeconds(15));
            wait.Until(ExpectedConditions.AlertIsPresent());

            IAlert alert = PropertiesCollection.Driver.SwitchTo().Alert();
            alert.SendKeys(proxy.Username + Keys.Tab + proxy.Password);
            alert.Accept();
        }
        catch { }

        return true;
    }
    catch (Exception exc)
    {
        Logger.Log("Could not start browser.", exc);
        return false;
    }
}
CodeGenius
  • 514
  • 1
  • 6
  • 21
0

You will need to use an AutoIT script.

WinWait("Authentication Required","","20")
If WinExists("Authentication Required") Then
   WinActivate("Authentication Required")
   Send($CmdLine[1])
   Send("{TAB}")
   Send($CmdLine[2])
   Send("{ENTER}")
EndIf

I am also working on another solution to use firefox about:cnofig properties to solve this issue. Will let you know if I come up with something.

Vikas Ojha
  • 6,742
  • 6
  • 22
  • 35
0

Try below code to set the proxy first and then autologin.

firefoxProfile.SetPreference("network.proxy.type", 1);
firefoxProfile.SetPreference("network.proxy.http", "add server name");
firefoxProfile.SetPreference("network.proxy.http_port", 8080);
firefoxProfile.SetPreference("network.proxy.ssl", "add server name");
firefoxProfile.SetPreference("network.proxy.ssl_port", 8080);
firefoxProfile.SetPreference("network.proxy.no_proxies_on", "add website url(s)");

Driver = new FirefoxDriver(firefoxProfile);

Hope this will work for you.

Thanks, Anshul

Rafayet Ullah
  • 1,108
  • 4
  • 14
  • 27
Anshul
  • 34
  • 2
-1
        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))
DevAnimal
  • 379
  • 2
  • 9