在 Chrome 上弹出处理 windows 身份验证

Posted

技术标签:

【中文标题】在 Chrome 上弹出处理 windows 身份验证【英文标题】:Handle windows authentication pop up on Chrome 【发布时间】:2013-10-21 20:45:41 【问题描述】:

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

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

WinWaitActive("Authentication Required","","120")
If WinExists("Authentication Required") Then
    Send("usernameTAB")
    Send("passwordEnter")
EndIf

任何让它工作的指针都会有所帮助。我没有使用username@password:google.com,因为重定向时会出现一些身份验证弹出窗口。

【问题讨论】:

你能更新一下窗口信息工具的内容吗?我想知道身份验证弹出窗口是否实际上是页面的一部分...此外,在 sqa.stackexchange(软件质量保证,以前是 selenium 问答网站)上有大量的 selenium 用户/专家。 不使用AutoIT还有另一种方法***.com/questions/11522434/… 【参考方案1】:

请试试这个,我的工作正常,我从 nuget 得到 AutoItX:

AutoItX.WinWait("title of your browser", "", 9); 
AutoItX.WinActivate("title of your browser");
AutoItX.Send("userid");
AutoItX.Send("TAB", 0);
AutoItX.Send("password");
AutoItX.Send("Enter", 0);

【讨论】:

【参考方案2】:

我能够让 AutoIt 通过文本而不是窗口标题来定位窗口。 AutoIt Window Information Tool 无法识别标题,但可以识别可见文本。

所以我把脚本改成:

WinWaitActive("","Authentication Required","120")
If WinExists("","Authentication Required") Then
    Send("usernameTAB")
    Send("passwordEnter")
EndIf

效果很好。

【讨论】:

我使用的是 Chrome 61.0.3163.91 64bit。 AutoIt v3 窗口信息工具将“Chrome 旧版窗口”显示为 Chrome 主窗口和基本身份验证对话框的可见文本和隐藏文本。我仍然尝试了您的示例代码,但它没有找到窗口。【参考方案3】:

首先你不需要AutoIt,你可以使用windows API。其次,Chrome 的基本身份验证对话框不是传统的 Window,因此您无法处理它(尝试使用 Spy++)。这会起作用的唯一原因是如果您没有在 SendKeys 调用之前将另一个窗口带到前台。您需要找到父 Chrome 窗口,可能类似于“URL - Google Chrome”,将其移到前面,然后发送密钥。这是一个例子:

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("]", "]");

希望对您有所帮助。

【讨论】:

如何等待登录提示出现? @PanuHaaramo 你可以使用一个有延迟的循环来重试 FindWindow 调用,直到它返回非 0 结果。 使用“Autoit v3 Window Info”工具检查 Chrome 主窗口和基本登录对话框的标题(和所有其他信息)是否相同。标题是 URL。目前我的代码等待标题成为正确的 URL,但是当标题更改时登录对话框还没有出现。我可以用睡眠来处理这个问题,但更好的解决方案是等到登录对话框可用。在 IE 和 Firefox 中,登录对话框是一个单独的窗口,但在 Chrome 中没有。【参考方案4】:

我使用了您的脚本并根据我的需要对其进行了一些扩展。用户和密码不是硬编码的,可以通过命令行或用户输入来设置。该脚本只需启动一次。

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 中设置了什么语言,并根据它设置窗口标题。

【讨论】:

以上是关于在 Chrome 上弹出处理 windows 身份验证的主要内容,如果未能解决你的问题,请参考以下文章

视频无法在 Firefox 上弹出引导模式

允许 webdriver 浏览器从 Windows 10 服务在桌面上弹出

extjs 怎么在window 上弹出一上新的window

脏表单 - 如何忽略 parent.window.location.href 上弹出的脏表单

ChromeDriver - 在 Selenium WebDriver 自动化上弹出禁用开发者模式扩展

为啥在终端上写 ppm 时会在终端上弹出 Perl 包管理器而不是 ppm>