Selenium WebDriver自动登录代理用户名和密码

时间:2014-03-28 07:46:58

标签: c# winforms selenium-webdriver

我在C#winforms应用程序中使用Selenium WebDriver。我在aoutologin中遇到了问题。 打开Firefox弹出警报后输入字段用户名和密码。

 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;

4 个答案:

答案 0 :(得分:1)

尝试以下代码首先设置代理,然后自动设置。

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);

希望这对你有用。

谢谢, Anshul

答案 1 :(得分:0)

您需要使用AutoIT脚本。

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

我还在研究使用firefox的另一个解决方案:cnofig属性来解决这个问题。如果我拿出一些东西会让你知道。

答案 2 :(得分:0)

这是一个旧线程,但这是我使其工作的方式:

  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;
    }
}

答案 3 :(得分:-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))