我想我的答案在那里,因为我想我已经解决了它。这个答案不需要通过URL传递凭证(对于那些不喜欢我的人)。它也不需要任何自定义的Firefox配置文件或扩展程序安装或包含在解决方案中,或安装到浏览器上,消除了跨机器兼容性问题。
我的问题是无法通过URL传递凭证来完成身份验证,因为登录名在代理之后。
所以,我转向Windows自动化工具包,并发现AutoIT。使用AutoIT和Selenium,您可以通过将用户名和密码发送到出现的窗口对话框来自动登录。这里是下面的步骤如何(注意是C#:
1 - 从http://www.autoitscript.com/site/autoit/downloads/
2下载的AutoIt - AutoIt的.dll文件添加到您的项目引用
右键单击引用,选择Add Reference。然后单击浏览按钮并浏览到dll位置(大多数默认安装将是c:\ Program Files(x86)\ AutoIt3 \ AutoItX \ AutoItX3.dll),并添加到项目中。
3 - 使用了AutoIt和硒这样的(假设你的网络驱动程序已经初始化):
//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。
快乐自动化。
就像更新一样,我能够解决这个问题。以前我尝试过sendkeys,但无法让它正常工作。我正在使用Sendkeys.sendWait(),在发送密钥之前使函数等待,但它仍然没有效果。通过在Sendkeys.sendWait(“text to put”)之前放入Thread.sleep(2000),它可以很好地工作。不知道为什么需要thread.sleep语句,因为sendwait应该覆盖,但它需要什么 – John 2012-08-09 17:27:59
另外我也尝试过WebDriverWait()函数,但它没有帮助 – John 2012-08-09 17:36:15