简体   繁体   中英

C# Selenium Firefox Set socks proxy with Auth

In C# I'm trying to set socks proxy with authentification in firefox.

This doesn't work

    Proxy proxy = new Proxy();
    proxy.SocksProxy = sProxyIP + ":" + sProxyPort;
    proxy.SocksUserName = sProxyUser;
    proxy.SocksPassword = sProxyPass;
    options.Proxy = proxy;
    _driver = new FirefoxDriver(service, options);

This doesn't work too

   profile.SetPreference("network.proxy.socks", sProxyUser + ":" + sProxyPass + "@" + sProxyIP + ":" + sProxyPort);
   profile.SetPreference("network.proxy.socks_port", sProxyPort);

How can I solve this?

As far as I know you can't do it in that way with Firefox. You need to add a new Firefox profile and then to work with it with Selenium. On this profile you need to save the proxy information and to save the username and password.

You can set a Firefox profile following this steps link . Then it is easy to do the job. I use that code:

FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);

Then you would have to suppress the alert for username and password verification. You can use two ways of doing that. The first one is to do it programmatically. Something like that:

 var alert = driver.SwitchTo().Alert();
 alert.Accept();

The other way is to do that from Firefox Profile Settings.

After a lot of researching, here is the solution I decided to go with.

It's a dirty hack, but it works.

I used AutoitX in order to automate the proxy auth window but had to use System.Windows.Automation in order to get the right auth window since my app will be multithreaded.

sProxyIP = "154.5.5.5";
sProxyUser = "user here";
sProxyPass = "pass here";
sProxyPort = 4444;

//Set proxy
profile.SetPreference("network.proxy.socks", sProxyIP);
profile.SetPreference("network.proxy.socks_port", sProxyPort);


//deal with proxy auth
_driver.Manage().Timeouts().PageLoad = TimeSpan.FromMilliseconds(0);
WebsiteOpen(@"https://somewebsite.com/");
AuthInProxyWindow(sProxyUser, sProxyPass);
_driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(60);



void ProxyAuthWindow(string login, string pass)
{
    try
    {   
        //wait for the auth window
        var sHwnd = AutoItX.WinWait("Authentication Required", "", 2);
        AutoItX.WinSetOnTop("Authentication Required", "", 1);

        //we are using Windows UIA so we make sure we got the right auth
         //dialog(since there will be multiple threads we can easily hit the wrong one)
        var proxyWindow = AutomationElement.RootElement.FindFirst(TreeScope.Subtree,
            new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass"));
        string hwnd = "[handle:" + proxyWindow.Current.NativeWindowHandle.ToString("X") + "]";
        AutoItX.ControlSend(hwnd, "", "", login, 1);
        AutoItX.ControlSend(hwnd, "", "", "{TAB}", 0);
        AutoItX.ControlSend(hwnd, "", "", pass, 1);
        AutoItX.ControlSend(hwnd, "", "", "{ENTER}", 0);
    }
    catch
    {

    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM