Search code examples
c#seleniumselenium-webdrivergeckodriverselenium-firefoxdriver

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?


Solution

  • 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
        {
    
        }
    
    
    }