Sign in to follow this  
Followers 0
imacleod

Window title checking

7 posts in this topic

I had some code which loops through “If WinExists ()… else if winexists()“ logic and worked OK, except the window title could change whilst in the middle of the options being evaluated, leading to errors. I tried setting a variable using WinGetTitle and using a Case satement, however, as that looks at the ACTIVE window it means the browser is no longer usable for other tasks.

I’m sure there has a better way of checking a window title at a point in time against a given set of options that I’m overlooking here?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If it has similar text you can simply use the second parameter in the WinGetTitle call, or if the verbiage of the title changes only slightly you can use the Opt(WinTitleMatchMode) option (both in the help file).

Or, look at WinGetHandle in the help file and forego the title altogether:

$hWnd = WinGetHandle("[CLASS:Notepad]", "")
ConsoleWrite($hWnd & @CRLF)

Sleep(10000) ;change title of Notepad window

ConsoleWrite($hWnd & @CRLF)
Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Cheers, WinGetHandle looks like it's doing the job for me.

I thought it was working, but it's not.

Am I doing something wrong here? :

Global $hWnd = WinGetHandle("LIST page","")

Global $sWinTitle = WinGetTitle($hWnd)

 

Window focus no longer stays with the window/tab the code is supposed to be running against.

The script works fine, as long as I don't do anything else with the PC!

Here's the full script, I'm so tierd of trying to get this to work "properly"; any constructive criticism welcomed.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=SIDA_batch_ID_extension.v20130514.exe
#AutoIt3Wrapper_Outfile_x64=SIDA_batch_ID_extension.exe
#AutoIt3Wrapper_Res_Description=SIDA batch ID extension
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <File.au3>
#include <String.au3>
#include <IE.au3>
#include <Array.au3>
#include <Date.au3>

;Debug y/n (do not remove the leading #)
#AutoIt3Wrapper_run_debug_mode = y
Opt("MustDeclareVars", 1)
Local Const $CSVFILE = "renewals.csv"
Local Const $DELIM = "," ;the delimiter in the CSV file
Local $i = 0
Local $sLogFileName = "import_log."
Local $iRunNum = 1

While FileExists($sLogFileName & $iRunNum & ".txt")
    $iRunNum += 1
WEnd
$sLogFileName = $sLogFileName & $iRunNum & ".txt"

;Create log file and enter current date and time
FileOpen($sLogFileName, 1)
FileWriteLine($sLogFileName, _DateTimeFormat(_NowCalc(), 0))
Local $answer = MsgBox(4, "SIDA batch ID extension", "This script will extend the expiration date for IDs contained in a file named 'renewals.csv' located in the same folder as this script was run from." & @CRLF & @CRLF & "Please ensure any existing SIDA sessions/windows are closed before proceeding." & @CRLF & @CRLF & "Run the script?")
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    MsgBox(0, "SIDA batch ID extension", "Update canceled!")
    FileWriteLine($sLogFileName, "Update canceled by user, from GUI.")
    FileClose($sLogFileName)
    Exit
 EndIf

 ;Check for duplicate instances
 if WinExists("SIDA") Then
    MsgBox(0, "SIDA batch ID extension", "SIDA instance already running" & @CRLF & @CRLF & "Please close that screen, and try again")
    FileWriteLine($sLogFileName, "SIDA instance already running - exiting")
    FileClose($sLogFileName)
    Exit
 EndIf

; open the SIDA screen
ShellExecute("iexplore", "http://***App URL***")

;open CSV file for reading
Local $file = FileOpen($CSVFILE, 0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open the file 'renewals.csv'.")
    FileWriteLine($sLogFileName, "Unable to open the file 'renewals.csv'.")
    FileClose($sLogFileName)
    Exit
Else
    ; Read in lines of comma seperated IDs until the EOF is reached
    While 1
        Local $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        Local $aIDs = StringSplit($line, $DELIM)
        For $i = 1 To $aIDs[0]
            ;Wait until the tab titled 'SIDA LIST page' exists
            WinWait("SIDA LIST page - Windows Internet Explorer")
            ;attach to the TAB with 'SIDA LIST page' as title
            Local $oIE = _IEAttach("SIDA LIST page")
            ;Get a handle for the window, indedependent of the title
            Global $hWnd = WinGetHandle("SIDA LIST page","")

            ;"ACQUSER" is the name of the ID input box, get a reference for it
            Local $oText = _IEGetObjByName($oIE, "ACQUSER")
            ; Assign input focus to the field and then send the text string
            _IEAction($oText, "focus")
            _IEFormElementSetValue($oText, $aIDs[$i])
            ;"ACQUIRE" is the name of the "Update" button, get a reference for it
            Local $oUpdate = _IEGetObjByName($oIE, "ACQUIRE")
            _IEAction($oUpdate, "click")
            ;Page is redrawn, select action based on window titles
            While 1
               WinWaitNotActive("SIDA LIST page - Windows Internet Explorer")
               Global $sWinTitle = WinGetTitle($hWnd)
               Select
                  Case $sWinTitle = "SIDA - Update/Extend This Userid - Windows Internet Explorer"
                     Local $oIEextend = _IEAttach("SIDA - Update/Extend This Userid")
                     ;Define an object containing the HTML from the page
                     Local $oPage = _IEDocReadHTML($oIEextend)
                     ; Change the 'dev.checked' variable check so that it does not trigger a 'help' page
                     Local $oPage_u = StringRegExpReplace($oPage, "dev\.checked", "devX\.checked")
                     ;Display the page with the doctored variable
                     _IEDocWriteHTML($oIEextend, $oPage_u)
                     ;"UPDATE" is the name of the "Submit request" button
                      Local $oSubmit = _IEGetObjByName($oIEextend, "UPDATE")
                     _IEAction($oSubmit, "click")

                     ;Page is redrawn
                     WinWaitNotActive("SIDA - Update/Extend This Userid - Windows Internet Explorer")
                     $sWinTitle = WinGetTitle($hWnd)
                     Select
                        case $sWinTitle = "Confirm Add/Update/Extend Action - Windows Internet Explorer"
                           Local $oIEconfirm = _IEAttach("Confirm Add/Update/Extend Action")
                          ;Click the "Confirm" button...
                           Local $oConfirm = _IEGetObjByName($oIEconfirm, "action")
                           _IEAction($oConfirm, "click")
                           Sleep(100)
                           ; ...and "Continue" buttons
                           Local $oContinue = _IEGetObjByName($oIEconfirm, "SUB")
                           While @error
                              Sleep(100)
                              Local $oContinue = _IEGetObjByName($oIEconfirm, "SUB")
                           WEnd
                           _IEAction($oContinue, "click")
                           ;log the ID as processed
                           FileWriteLine($sLogFileName, _NowTime() & " " & $aIDs[$i] & " OK")
                           ExitLoop
                        Case $sWinTitle = "Errors found in Joint Venture information - Windows Internet Explorer"
                            ;Invalid Company name/job description?
                            ;Open a new 'SIDA LIST page"
                            _IENavigate($oIE, "http://***App URL***")
                            ;log the ID as having issues
                            FileWriteLine($sLogFileName, _NowTime() & " " & $aIDs[$i] & " errors found in JV information")
                            ExitLoop
                     EndSelect
                  Case $sWinTitle = "SIDA - Userid Sponsorship - Windows Internet Explorer"
                     ;Invalid ID in list,
                     ;Open a new 'SIDA LIST page"
                     _IENavigate($oIE, "http://***App URL***")
                     ;log the ID as having issues
                     FileWriteLine($sLogFileName, _NowTime() & " " & $aIDs[$i] & " issues with ID")
                     ExitLoop
                  Case $sWinTitle = "SIDA - Unable to process request because... - Windows Internet Explorer"
                     ;Not flagged as a SIDA employee?
                     ;Open a new 'SIDA LIST page"
                     _IENavigate($oIE, "http://***App URL***")
                     ;log the ID as having issues
                     FileWriteLine($sLogFileName, _NowTime() & " " & $aIDs[$i] & " Unable to process request; not flagged as a SIDA employee?")
                     ExitLoop
                  Case $sWinTitle = "SIDA - Userid Sponsorship - Windows Internet Explorer"
                     ;"Unable to process request because... Directry in use, try again later."?
                     ;Open a new 'SIDA LIST page"
                     _IENavigate($oIE, "http://***App URL***")
                     ;log the ID as having issues
                     FileWriteLine($sLogFileName, _NowTime() & " " & $aIDs[$i] & " Directry in use? Re-submit ID")
                     ExitLoop
                  Case Else
                     ;Unexpected screen displayed
                     ;Open a new 'SIDA LIST page"
                     _IENavigate($oIE, "http://***App URL***")
                     ;log the ID as having issues
                     FileWriteLine($sLogFileName, _NowTime()& " " & $aIDs[$i] & " unexpected SIDA screen, check ID")
                     ExitLoop
               Sleep(100)
               EndSelect
            WEnd
        Next
    WEnd
EndIf
MsgBox(0, "SIDA batch ID extension", $i - 1 & " IDs in the file 'renewals.csv' processed." & @CRLF & @CRLF & "Check the log file for IDs marked as anything other than 'OK'")
WinClose("SIDA LIST page - Windows Internet Explorer")
FileWriteLine($sLogFileName, _DateTimeFormat(_NowCalc(), 0))
FileClose($sLogFileName)
FileClose($file)
Edited by imacleod

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

As my previous post was an edit, rather than a post, it's under the radar in the queue/pile...this should sort that.

Edited by imacleod

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

USe the _ie functions, rather than shell, and win* funcs...will make life much easier...if you want to grab the hwnd, you can still do so through _iepropertyget.

_iecreate

When you have the object from creation, you can more easily do navigation, and not worry about the title...rather than attaching at some other point.

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Thanks, but I've tried using _ie functions for this and ran in to a brick wall due to some issue, discussed at length on here; I'll go back and have another look, but it seems whichever way I turn fix one thing and break something else :ermm:

Share this post


Link to post
Share on other sites

What is 'here', post a link, or define it.  The only reason I have ever seen _IEcreate fail, is if there are already many IE processes...I generally kill them all, prior to starting (scripted processkill)


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
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
Sign in to follow this  
Followers 0