6

I am trying to handle the basic authentication pop up for my Selenium webdriver scripts using AutoIt. I wrote a script for Firefox and Internet Explorer but it doesn't work for Chrome.

When I tried identifying the authentication pop up on Chrome using AutoIt Window Information Tool it came up empty. I am using following AutoIt script:

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

Any pointers to get this to work would be helpful. I am not using username@password:google.com because some authentication pop ups appear on redirection.

4

4 回答 4

3

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

所以我将脚本更改为:

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

它工作得很好。

于 2013-10-15T06:07:25.363 回答
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("]", "{]}");
}

希望有帮助。

于 2015-11-25T13:49:27.890 回答
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 中设置的语言,并根据它设置窗口标题。

于 2013-11-07T06:21:23.113 回答
0

请试试这个,我的工作,我从 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);
于 2018-07-23T05:59:25.763 回答