2013-10-14 29 views
6

我正在尝试使用AutoIt处理我的Selenium webdriver脚本弹出的基本身份验证。我为Firefox和Internet Explorer编写了一个脚本,但它不适用于Chrome。Chrome上弹出处理Windows身份验证

当我尝试识别使用AutoIt Window Information Tool在Chrome上弹出的身份验证时,它变为空。我正在使用以下AutoIt脚本:

WinWaitActive("Authentication Required","","120") 
If WinExists("Authentication Required") Then 
    Send("username{TAB}") 
    Send("password{Enter}") 
EndIf 

任何指示器来获得此工作将有所帮助。我没有使用[email protected]:google.com,因为一些身份验证弹出窗口出现在重定向上。

+0

您可以使用窗口信息工具进行更新吗?我想知道,如果认证弹出窗口实际上是页面的一部分...此外,在sqa.stackexchange(软件质量保证,以前是一个硒问答网站)有大量的硒用户/专家。 – Colyn1337

+0

有没有使用AutoIT的另一种方法https://stackoverflow.com/questions/11522434/how-to-handle-login-pop-up-window-using-selenium-webdriver/30067944#30067944 –

回答

3

我能够通过文本而不是窗口标题使AutoIt锁定目标窗口。 AutoIt窗口信息工具没有识别标题,但它确实识别了可见的文本。

所以我改变了脚本:

WinWaitActive("","Authentication Required","120") 
If WinExists("","Authentication Required") Then 
    Send("username{TAB}") 
    Send("password{Enter}") 
EndIf 

它工作得很好。

+1

我使用Chrome 61.0 .3163.91 64bit。 AutoIt v3窗口信息工具在Chrome主窗口和基本authn对话框中显示“Chrome旧窗口”为可见文本和隐藏文本。我仍然试过你的示例代码,但它没有找到窗口。 –

0

我用你的脚本,并扩展它一点点,以满足我的需求。用户和密码不是硬编码的,可以通过命令行或用户输入进行设置。脚本只需要启动一次。

If(Not IsArray($CmdLine) Or $CmdLine[0] < 2) Then 
    $user = InputBox ("User", "Please enter your user", "") 
    $pass = InputBox ("Password", "Please enter your password", "", "*M") 
Else 
    $user = $CmdLine[1] 
    $pass = $CmdLine[2] 
EndIf 


While(True) 
    WinWaitActive("", "Authentifizierung erforderlich","120") 
    If WinExists("", "Authentifizierung erforderlich") Then 
     Send($user) 
     Send("{TAB}") 
     Send($pass) 
     Send("{Enter}") 
     Sleep(1000) 
    EndIf 
WEnd 

下一步是确定在chrome中设置了哪种语言并设置窗口标题依赖于它。

2

首先你不需要AutoIt,你可以使用windows API。其次,Chrome的基本身份验证对话框不是传统的窗口,因此您无法获取它的句柄(尝试使用Spy ++)。这可以起作用的唯一原因是,如果您在SendKeys呼叫之前未将其他窗口置于前台。您需要找到父级Chrome窗口,可能类似于“URL - 谷歌浏览器”,将其移到前面,然后发送密钥。这里有一个例子:

using System; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

[DllImport("User32.dll")] 
private static extern bool SetForegroundWindow(IntPtr point); 

[DllImport("user32.dll")] 
private static extern IntPtr FindWindow(string className, string windowTitle); 

public static void SendBasicAuthentication(string username, string password, string windowTitle) 
{ 
    var hwnd = FindWindow(null, windowTitle); 
    if (hwnd.ToInt32() <= 0 || !SetForegroundWindow(hwnd)) return; 
    SendKeys.SendWait(username.EscapeStringForSendKeys()); 
    SendKeys.SendWait("{TAB}"); 
    SendKeys.SendWait(password.EscapeStringForSendKeys()); 
    SendKeys.SendWait("{ENTER}"); 
} 

static string EscapeStringForSendKeys(this string input) 
{ 
    // https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx 
    // must do braces first 
    return input.Replace("{", "{{}") 
     .Replace("}", "{}}") 
     .Replace("^", "{^}") 
     .Replace("%", "{%}") 
     .Replace("~", "{~}") 
     .Replace("(", "{(}") 
     .Replace(")", "{)}") 
     .Replace("[", "{[}") 
     .Replace("]", "{]}"); 
} 

希望有帮助。

+0

您将如何等待登录提示出现? –

+0

@PanuHaaramo你可以使用一个带有延迟的循环重试FindWindow调用,直到返回非0结果。 – scottrudy

+1

使用“Autoit v3窗口信息”工具检查Chrome主窗口和基本登录对话框的标题(和所有其他信息)是相同的。标题是网址。目前我的代码等待标题为正确的URL,但当标题更改时,登录对话框还没有出现。我可以通过睡眠来处理,但更好的解决方案是等待登录对话框可用。 在IE和Firefox登录对话框是一个单独的窗口,但不是在Chrome中。 –