C#Selenium Firefox设置具有身份验证的socks代理

C#Selenium Firefox设置具有身份验证的socks代理,c#,selenium,selenium-webdriver,geckodriver,selenium-firefoxdriver,C#,Selenium,Selenium Webdriver,Geckodriver,Selenium Firefoxdriver,在C#中,我尝试在firefox中使用身份验证设置socks代理 这不管用 Proxy proxy = new Proxy(); proxy.SocksProxy = sProxyIP + ":" + sProxyPort; proxy.SocksUserName = sProxyUser; proxy.SocksPassword = sProxyPass; options.Proxy = proxy; _driver = new FirefoxD

在C#中,我尝试在firefox中使用身份验证设置socks代理

这不管用

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

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

我该如何解决这个问题呢?

据我所知,Firefox不能以这种方式解决。您需要添加一个新的Firefox配置文件,然后使用Selenium。在此配置文件上,您需要保存代理信息以及用户名和密码

您可以按照以下步骤设置Firefox配置文件。这样做很容易。我使用的代码是:

FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);
然后,您必须取消用户名和密码验证警报。您可以使用两种方法来实现这一点。第一个是以编程方式进行。诸如此类:

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

另一种方法是通过Firefox配置文件设置来实现。

据我所知,Firefox不能以这种方式实现。您需要添加一个新的Firefox配置文件,然后使用Selenium。在此配置文件上,您需要保存代理信息以及用户名和密码

您可以按照以下步骤设置Firefox配置文件。这样做很容易。我使用的代码是:

FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);
然后,您必须取消用户名和密码验证警报。您可以使用两种方法来实现这一点。第一个是以编程方式进行。诸如此类:

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

另一种方法是通过Firefox配置文件设置来实现。

经过大量研究,我决定采用以下解决方案

这是一个肮脏的黑客,但它的工作

我使用了
AutoitX
来自动化代理身份验证窗口,但由于我的应用程序将是多线程的,因此必须使用
System.Windows.Automation
来获得正确的身份验证窗口

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
    {

    }


}

经过大量研究,以下是我决定采用的解决方案

这是一个肮脏的黑客,但它的工作

我使用了
AutoitX
来自动化代理身份验证窗口,但由于我的应用程序将是多线程的,因此必须使用
System.Windows.Automation
来获得正确的身份验证窗口

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
    {

    }


}

这对我来说是不可能的,因为我必须通过编程更改代理IP、端口、用户和密码。我看到你找到了一个解决方案,但如果其他人找不到,他们可以创建几个Firefox配置文件,每个配置文件都有不同的代理IP、用户和密码。然后很容易通过编程更改配置文件。这对我来说是不可能的,因为我必须通过编程更改代理IP、端口、用户和密码。我看到你找到了一个解决方案,但如果其他人找不到,他们可以创建几个Firefox配置文件,每个配置文件都有不同的代理IP、用户和密码。这样就很容易通过编程更改配置文件。