Jump to content

IE automation : automate IE credentials windows


Recommended Posts

Hi,

I'm facing the same issue as in that thread.

It's caused by limitations of that IE specification.

You just can't fill in the logon information by only using the URL.

I've decided to go brute force, and simply have autoIT enter the information in the fields, and then vaidate the popup.

However, I'm facing a rather ridiculous issue : I just can't manage to detect when that darn popup is ready.

Here is my code :

#include <IE.au3>

$CODE_OK = 0
$CODE_TERMINATION = 3



Func UserStatus_CredentialsPopup($username, $password, $visible)

    $title = "Connect to directory.accenture.com"


    if WinWaitActive($title, "", 3) = 0 then 
        MsgBox(0, "Error", "UserStatus_CredentialsPopup: Couldn't get the focus on the user status window"&@CRLF&"Aborting script")
        return $CODE_TERMINATION
    EndIf

    MsgBox(0, "Info", "the window is active")

    Send($password)
    Sleep(500)
    Send("{TAB}")
    Send("{TAB}")
    Send("{TAB}")
    Send("{TAB}")
    Send($username)
    Sleep(500)
    Send("{TAB}")
    Send("{TAB}")
    Send("{TAB}")
    Sleep(500)          
    

EndFunc 
    

Func _IECreateSafe($visible)

    Local $o_IE
    If Not IsObj($o_IE) Then
         $sRandom = "about:blank-" & Random(1000000, 9999999, 1)
         $sIE_pid = Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe " & $sRandom, "", $visible)
         Local $i_timer = TimerInit()
         While Not IsObj($o_IE)
              If Int(TimerDiff($i_timer) / 1000) > 45 Then _
                  ConsoleWrite("Failed to open browser" & @CRLF)
              $o_IE = _IEAttach($sRandom, "url")
              Sleep(100)
          WEnd
    EndIf
    
    return $o_IE
EndFunc


Func Main()
    
    $username = "user"
    $password = "passw"
    $visible = 1
    
    Local $o_IE = _IECreateSafe($visible)


    $url = "https://directory.accenture.com/userstatus"
    _IENavigate($o_IE, $url, 1)


    $result = UserStatus_CredentialsPopup($username, $password, $visible)

EndFunc

Main()

What confuses me most is that the "WinWaitActive" seems to be triggered only after I close the credentials popup, as if the display of that popup was part of a potential "_IELoadWait"

Also, when trying to test what window has the focus (for example with the code below), you find that it's the popup window that's active, as expected... So why won't it be spotted by my application?

#include <WINApi.au3>
$windowHandle = _WinAPI_GetForegroundWindow()
$text = _WinAPI_GetWindowText($windowHandle)
Link to comment
Share on other sites

I'm not quite sure but it's one of those blocking boxes that halt your script execution, even when you specify False as the third parameter of _IENavigate() function. By the way, the OP in the thread you've linked to said that it works with Firefox so you can try it, there is FF.au3 library in the example script forum, made by Stilgar.

Link to comment
Share on other sites

Take a look at the second example for _IEAction that shows a method of preventing your script from stalling in a situation like this.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

Take a look at the second example for _IEAction that shows a method of preventing your script from stalling in a situation like this.

Thanks DaleHohm. Your answer would have allowed me to have better control over my overall process, by controlling the actions step by step. I solved my issue more simply by changing the "blocking" parameter of _IENavigate to 0. By not waiting for the page to be loaded, the script doesn't stall. for better security, I'm still free to use _IELoadWait *after* the credentials popup.

Here is the final code.

The function _IENavigateSafe_CredentialsPopup is an ideal snippet for those who want to go past a webpage that prompts for credentials in a popup window.

#include-once


#include <IE.au3>


$CODE_OK                        = 0
$CODE_NONBLOCKING_FAILURE       = 1
$CODE_CREDENTIALS_NOT_ASKED     = 2
$CODE_TERMINATION               = 3
$CODE_INCORRECT_CREDENTIALS     = 4
$CODE_OBSOLETE_LAYOUT           = 5

$loadTimeout = 3000 ; time to wait (in milliseconds) before we consider a page is too long to load
$activeTimeout = 3 ; time to wait (in seconds) before we consider a window is too long to activate





func LogError($msg, $showInPopup)
        if $showInPopup then _
            MsgBox(0, "Error", $msg)
        ConsoleWrite($msg&@CRLF)
EndFunc





Func TestPageSuccess (ByRef $o_IE, $textWhenSuccess, $textWhenFailure, $visible)

    _IELoadWait($o_IE, $loadTimeout, $loadTimeout) ;test
    ;ConsoleWrite(_IEBodyReadText($o_IE)&@CRLF) ; debug
    
    if $textWhenSuccess = "" and $textWhenFailure = "" then _ ;we don't test if no conditions have been provided
        return $CODE_OK
        
    If StringInStr(_IEBodyReadText($o_IE), $textWhenFailure) and  $textWhenFailure <> "" Then
        LogError("TestPageSuccess: detected bad string in page:"&$textWhenFailure, $visible)
        Return $CODE_NONBLOCKING_FAILURE     
    ElseIf StringInStr(_IEBodyReadText($o_IE), $textWhenSuccess)  and  $textWhenSuccess <> ""  Then
        LogError("TestPageSuccess: found '"&$textWhenSuccess&"'", $visible)
        Return $CODE_OK     
    Else
        LogError("TestPageSuccess: Unexpected result. Page contains neither '"&$textWhenFailure&"' nor '"&$textWhenSuccess&"'", true)
        Return $CODE_TERMINATION   
    EndIf
    
    return $CODE_TERMINATION ; in case not all execution paths have been anticipated
EndFunc


Func _IECreateSafe($visible, $timeout = 10)

    Local $o_IE
    If Not IsObj($o_IE) Then
        $sRandom = "about:blank-" & Random(1000000, 9999999, 1)  ; choose an unmistakable name for our IE window
        $sIE_pid = Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe " & $sRandom, "", $visible) ;create the actual IE window
        Local $i_timer = TimerInit()
        While Not IsObj($o_IE) ;the Internet Explorer process has to be ready before we do anything
            If Int(TimerDiff($i_timer) / 1000) > $timeout Then
                LogError("Failed to open an IE browser in less than "&$timeout&" seconds. Aborting.", true)
                return 0
            endif
            $o_IE = _IEAttach($sRandom, "url")  ; try to use the IE window we just created
            Sleep(200) ; don't try too often
        WEnd
    EndIf
    
    return $o_IE
EndFunc





Func _IENavigateSafe_CredentialsPopup($o_IE, $url, $textWhenSuccess, $textWhenFailure, $username, $password, $visible)

    MsgBox(0, "Info", "Please don't move your mouse until prompted again to do so.", 5)
    _IENavigate($o_IE, $url, 0) ; it is ESSENTIAL that we don't wait for the page to be loaded (the credentials window is part of the loading phase)
    
    $credentialsPopupTimeout = 5
    $title = "Connect to"
    $username_field = "Edit2" ;that's how it's named in the standard Internet Explorer credentials window
    $password_field = "Edit3" ;that's how it's named in the standard Internet Explorer credentials window


    if WinWaitActive($title, "", $credentialsPopupTimeout) = 0 then ; the credentials popup doesn't appear
        _IELoadWait($o_IE, $loadTimeout)
        if StringInStr(_IEBodyReadText($o_IE), $textWhenSuccess)  and  $textWhenSuccess <> "" Then ;in case we miraculously went through the credentials popup
            return $CODE_OK
        Else
            LogError("UserStatus_CredentialsPopup: Couldn't get the focus on the user status window"&@CRLF&"Cancelled navigation to page", $visible)
            return $CODE_TERMINATION
        EndIf
    EndIf

    ControlSend($title, "", $username_field, "^A") ; select the text
    ControlSend($title, "", $username_field, $username)
    ControlSend($title, "", $password_field, "^A") ; select the text
    ControlSend($title, "", $password_field, $password)

    ControlSend($title, "", $password_field, "{ENTER}") ; presse enter


    ; test if the credentials popup was displayed again (wrong credentials)
    if WinWaitActive($title, "", 2) then ; very short timeout
        ControlSend($title, "", $username_field, "{ESC}") ; select the text
        LogError("Wrong credentials when trying to log onto '"&$url&"'", $visible)
        return $CODE_INCORRECT_CREDENTIALS
    EndIf

    MsgBox(0, "Info", "You can start moving your mouse again", 3) ; there is atimeout, in case the user didn't see it

    return TestPageSuccess ($o_IE, $textWhenSuccess, $textWhenFailure, $visible)


    ;return $CODE_OK

EndFunc 



$visible = 0 ; hide execution
Local $o_IE = _IECreateSafe($visible)

$url = "https://anyPageWithCredentialsPopup.com
$textWhenSuccess = "This text is expected on the page if you succeed"
$textWhenFailure = "not authorized" ; this text appears on most websites when you fail entering your credentials

$result = _IENavigateSafe_CredentialsPopup($o_IE, $url, $textWhenSuccess, $textWhenFailure, "username", "password", $visible)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...