使用webdriver和弹出窗口进行http授权

我正在尝试使用Selenium WebDriver使用用户名和密码自动登录到站点。 我完成了我的研究,我不相信WebDriver支持这个function,所以我需要找到另一种方法。 我正在尝试自动登录的网站位于此处 。

当提示登录时,出现一个似乎不属于浏览器的弹出窗口。 我正在使用Firefox和Chrome。 看来可能需要Windows API? 我已经尝试在URL中传递凭据,但这不起作用。 还尝试了sendkeys,但收到了Windowsexception,表明应用程序没有接受Windows消息。 我也尝试使用driver.windowhandles切换当前handle但弹出窗口似乎不是新句柄。

有人有什么想法吗? 我有点卡住了。 进入弹出窗口的初步代码是:

 IWebDriver driver = new FirefoxDriver(); driver.Navigate().GoToUrl("http://www.portal.adp.com"); string currentWindow = driver.CurrentWindowHandle; IWebElement userLogin = driver.FindElement(By.Id("employee")); userLogin.Click(); 

您看到的弹出窗口是由Web服务器提示的,是一个身份validation提示。 Selenium不支持此操作。

处理此限制的方法之一是在URL中传递用户和密码,如下所示:

http://user:password@example.com

更多信息请访问: http : //aleetesting.blogspot.in/2011/10/selenium-webdriver-tips.html

我想要我的答案因为我认为我已经解决了。 这个答案不需要通过URL传递凭据(对于那些无法喜欢我的人)。 它也不需要在解决方案中安装或包含任何自定义Firefox配置文件或扩展,也不需要安装到浏览器上,从而消除了跨机器兼容性问题。

我的问题是,通过URL传递凭据无法完成身份validation,因为登录位于代理后面。

所以,我转向Windows自动化工具包,发现了AutoIT。 使用AutoIT和Selenium,您可以通过将用户名和密码发送到出现的Windows对话框来自动登录。 这是如何(请注意以下步骤适用于c#:

1 – 从http://www.autoitscript.com/site/autoit/downloads/下载AutoIT

2 – 将autoit .dll添加到项目引用中。

右键单击引用,选择“添加引用”。 接下来单击浏览按钮并浏览到dll位置(大多数默认安装它将是c:\ Program Files(x86)\ AutoIt3 \ AutoItX \ AutoItX3.dll),然后添加到项目中。

3 – 像这样使用AutoIT和Selenium(假设您的Web驱动程序已经初始化):

  //Initialize AutoIT var AutoIT = new AutoItX3(); //Set Selenium page load timeout to 2 seconds so it doesn't wait forever Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2)); //Ingore the error try { Driver.Url = url; } catch { return; } //Wait for the authentication window to appear, then send username and password AutoIT.WinWait("Authentication Required"); AutoIT.WinActivate("Authentication Required"); AutoIT.Send("username"); AutoIT.Send("{TAB}"); AutoIt.Send("password"); AutoIT.Send("{ENTER}"); //Return Selenium page timeout to infinity again Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(-1)); 

无论如何,就是这样,它就像一个魅力:)

另请注意,有些特殊字符需要使用序列“{x}”在AutoIT中进行转义。 例如,如果您的密码是“!tRocks”,则需要将其作为“{!} tRocks”传递给AutoIT。

快乐的自动化。

  FirefoxProfile profile = new FirefoxProfile(); profile.SetPreference("network.http.phishy-userpass-length", 255); profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname); Driver = new FirefoxDriver(profile); 

hostname是您的URL(example.com)然后尝试

 Driver.Navigate().GoToUrl(http://user:password@example.com); 

我刚刚完成了一个原型项目,该项目应该能够处理这种情况。

它利用流行的开源代理BrowserMob来执行身份validation。

SeleniumBasicAuthWrapper希望它有所帮助! 它仍在进行中,但希望在不久的将来我们能够解决任何问题或缺陷。