简体   繁体   English

C# Selenium Firefox设置socks代理带Auth

[英]C# Selenium Firefox Set socks proxy with Auth

In C# I'm trying to set socks proxy with authentification in firefox.在 C# 中,我试图在 firefox 中设置带有身份验证的 socks 代理。

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.据我所知,你不能用 Firefox 那样做。 You need to add a new Firefox profile and then to work with it with Selenium.您需要添加一个新的 Firefox 配置文件,然后与 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 .您可以按照此步骤链接设置 Firefox 配置文件。 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.另一种方法是从 Firefox 配置文件设置中执行此操作。

After a lot of researching, here is the solution I decided to go with.经过大量研究,这里是我决定 go 的解决方案。

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.我使用AutoitX来自动化代理身份验证 window 但必须使用System.Windows.Automation才能获得正确的身份验证 window 因为我的应用程序将是多线程的。

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
    {

    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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