Jump to content

UI Automation, wait for element to be visible.


Recommended Posts

Is there a function in UI Automation that I can use to pause a script until an element is loaded or becomes visible before proceeding with the rest of the code?  We have thousands of computers and some of them are not the latest and greatest so it takes anywhere from an extra 15 seconds to as high as 120 seconds before the next element/control becomes visible on some devices.  This is wreaking havoc on my newbie brain.  I have included the part of the code that I need help with below.  Any insights and advices are much appreciated.

ConsoleWrite("--- Find Windows Security Prompt window/control ---" & @CRLF)
    While 1
        If Not WinWaitActive("Windows Security") Then
            Sleep(500)
        Else
            ExitLoop
        EndIf
    WEnd

    Local $pCondition3
    $oUIAutomation.CreatePropertyCondition($UIA_NamePropertyId, "Windows Security", $pCondition3)
    If Not $pCondition3 Then Return ConsoleWrite("$pCondition3 ERR" & @CRLF)
    ConsoleWrite("$pCondition3 OK" & @CRLF)
    Sleep(500)

    Local $pWindow2, $oWindow2
    $oWindow1.FindFirst($TreeScope_Descendants, $pCondition3, $pWindow2)
    $oWindow2 = ObjCreateInterface($pWindow2, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
    If Not IsObj($oWindow2) Then Return ConsoleWrite("$oWindow2 ERR" & @CRLF)
    ConsoleWrite("$oWindow2 OK" & @CRLF)
    Sleep(500)

    ConsoleWrite("--- Find Confirm Certificate OK Button window/control ---" & @CRLF)

    Local $pCondition4
    $oUIAutomation.CreatePropertyCondition($UIA_AutomationIdPropertyId, "OkButton", $pCondition4)
    If Not $pCondition4 Then Return ConsoleWrite("$pCondition4 ERR" & @CRLF)
    ConsoleWrite("$pCondition4 OK" & @CRLF)
    Sleep(500)

    Local $pButton3, $oButton3
    $oWindow2.FindFirst($TreeScope_Descendants, $pCondition4, $pButton3)
    $oButton3 = ObjCreateInterface($pButton3, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
    If Not IsObj($oButton3) Then Return ConsoleWrite("$oButton3 ERR" & @CRLF)
    ConsoleWrite("$oButton3 OK" & @CRLF)
    Sleep(500)

    ConsoleWrite("--- Invoke Confirm Certificate OK Button Pattern (action) Object ---" & @CRLF)

    Local $pInvokePattern3, $oInvokePattern3
    $oButton3.GetCurrentPattern($UIA_InvokePatternId, $pInvokePattern3)
    $oInvokePattern3 = ObjCreateInterface($pInvokePattern3, $sIID_IUIAutomationInvokePattern, $dtag_IUIAutomationInvokePattern)
    If Not IsObj($oInvokePattern3) Then Return ConsoleWrite("$oInvokePattern3 ERR" & @CRLF)
    ConsoleWrite("$oInvokePattern3 OK" & @CRLF)
    $oInvokePattern3.Invoke()
    Sleep(2000)
    
    ;========= Need a pause/function here to verify the NEXT button is visible before proceeding ======
    Sleep(30000) ;using sleep to test
    ;==================================================================================================

    ConsoleWrite( "--- Find Next Button window/control ---" & @CRLF )

    Local $pCondition5
    $oUIAutomation.CreatePropertyCondition( $UIA_NamePropertyId, "Next", $pCondition5 )
    If Not $pCondition5 Then Return ConsoleWrite( "$pCondition5 ERR" & @CRLF )
    ConsoleWrite( "$pCondition5 OK" & @CRLF )
    Sleep(500)

    Local $pButton4, $oButton4
    $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition5, $pButton4 )
    $oButton4 = ObjCreateInterface( $pButton4, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
    If Not IsObj( $oButton4 ) Then Return ConsoleWrite( "$oButton4 ERR" & @CRLF )
    ConsoleWrite( "$oButton4 OK" & @CRLF )
    Sleep(500)

    ConsoleWrite( "--- Invoke Next Button Pattern (action) Object ---" & @CRLF )

    Local $pInvokePattern4, $oInvokePattern4
    $oButton4.GetCurrentPattern( $UIA_InvokePatternId, $pInvokePattern4 )
    $oInvokePattern4 = ObjCreateInterface( $pInvokePattern4, $sIID_IUIAutomationInvokePattern, $dtag_IUIAutomationInvokePattern )
    If Not IsObj( $oInvokePattern4 ) Then Return ConsoleWrite( "$oInvokePattern4 ERR" & @CRLF )
    ConsoleWrite( "$oInvokePattern4 OK" & @CRLF )
    $oInvokePattern4.Invoke()
    Sleep(2000)

 

Edited by LisHawj
Link to comment
Share on other sites

You can always wrap it in a loop with a Sleep call ;)

;~ Local $iCount = 0

; While we haven't found the next button
While Not IsObj($oButton4)
    ; Search for it
    $oDesktop.FindFirst( $TreeScope_Descendants, $pCondition5, $pButton4 )
    ; Attempt to create the object
    $oButton4 = ObjCreateInterface( $pButton4, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement )
    ; If we found it (it's an object)
    If IsObj( $oButton4 ) Then
        ConsoleWrite( "$oButton4 OK" & @CRLF)
    Else
        ; Wait a little before retrying so we don't kill the old computers
        Sleep(500)
        
        ; Optionally, count the number of times this happens...
        ;~ $iCount += 1
        ; If it loops too many times, return an error
        ;~ If $iCount >= 10 Then Return SetError(1, 0, False)
    EndIf
WEnd

Edit: And this works for finding a lot of things... I made this into a function to wait for elements/windows/etc to appear, cause I got bored...

Spoiler

 

; #FUNCTION# ====================================================================================================================
; Name ..........: _UIA_FindFirst
; Description ...: Finds the first object matching $pCondition
; Syntax ........: _UIA_FindFirst($oParent, $pCondition[, $iTimeOut = Default[, $TreeScope = $TreeScrope_Descendants]])
; Parameters ....: $oParent             - the parent object to search.
;                  $pCondition          - a condition to use to search.
;                  $iTimeOut            - [optional] how many ms to wait before erroring. Default is Default (never timeout).
;                  $TreeScope           - [optional] scope of the search. Default is $TreeScrope_Descendants.
; Return values .: Success - the first object found
;                  Failure - False and sets @error to 1: waited longer than $iTimeOut
; Author ........: Seadoggie01
; Modified ......: 
; Remarks .......: 
; Related .......: 
; Link ..........: 
; Example .......: No
; ===============================================================================================================================
Func _UIA_FindFirst($oParent, $pCondition, $iTimeOut = Default, $TreeScope = $TreeScrope_Descendants)
    
    Local $pRet, $oRet, $iCurr = 0
    
    $oParent.FindFirst($TreeScope, $pCondition, $pRet)
    While Not IsObj($oRet)
        $oRet = ObjCreateInterface($pRet, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
        If Not IsObj($oRet) Then
            
            $iCurr += 250
            Sleep(250)
            
            If Not IsKeyword($iTimeOut) And $iCurr >= $iTimeOut Then Return SetError(1, 0, False)
            
        EndIf
    WEnd
    
EndFunc
Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Not really proficient... bored and lazy. There's a Bill Gates quote for lazy people... but I'm too lazy to find it :D

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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...