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.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Pooja Mistry
  • 91
  • 1
  • 1
  • 5
  • Can you update with what the window info tool has? I'm wondering if the authentication popup is actually part of the page... Also, there's a great number of selenium users/experts at sqa.stackexchange (software quality assurance, formerly a selenium Q&A site). – Colyn1337 Oct 14 '13 at 17:09
  • There is another way without using AutoIT https://stackoverflow.com/questions/11522434/how-to-handle-login-pop-up-window-using-selenium-webdriver/30067944#30067944 – susan rheyas Feb 17 '18 at 00:38

4 Answers4

3

I was able to have AutoIt target the window by text, not window title. AutoIt Window Information Tool did not recognize the title but it did recognize the visible text.

So I changed the script to:

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

It worked fine.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Pooja Mistry
  • 91
  • 1
  • 1
  • 5
  • 1
    I'm using Chrome 61.0.3163.91 64bit. AutoIt v3 Window Info tool shows "Chrome Legacy Window" as Visible Text and Hidden Text for both the Chrome main window and for the basic authn dialog box. I still tried your sample code but it is not finding the window. – Panu Haaramo Sep 15 '17 at 12:56
3

First of all you don't need AutoIt, you can just use the windows API. Secondly, Chrome's basic authentication dialog is not a traditional Window, so you can't get a handle to it (Try using Spy++). The only reason this would work is if you didn't bring another window to the foreground prior to the SendKeys call. You need to find the parent Chrome window, which may be something like "URL - Google Chrome", move it to the front and then send keys. Here's an example:

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

Hope that helps.

scottrudy
  • 1,633
  • 1
  • 14
  • 24
  • How would you wait for the login prompt to appear? – Panu Haaramo Sep 15 '17 at 13:12
  • @PanuHaaramo you could just use a loop with a delay to retry the FindWindow call until it comes back with a non 0 result. – scottrudy Sep 18 '17 at 01:37
  • 1
    Checking with "Autoit v3 Window Info" tool the Title (and all the other info) are the same for the Chrome main window and the basic login dialog. Title is the URL. Currently my code waits for the Title to be the correct URL but the login dialog is not yet there when the Title changes. I can handle this with sleep but better solution would be to wait until login dialog is available. In IE and Firefox login dialog is a separate window but not in Chrome. – Panu Haaramo Sep 18 '17 at 06:33
0

I used your script and extended it a little bit for my needs. The user and passwort aren't hard coded and can be set by commandline or user input. The script only has to be started once.

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

The next step would be to figure out what language is set up in chrome and set the window title dependent on it.

Sven-Michael Stübe
  • 14,560
  • 4
  • 52
  • 103
0

Please try this, mine is working, I got AutoItX from nuget:

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);
Haryono
  • 2,184
  • 1
  • 21
  • 14