Jump to content

AutoIt Wait until browser window is visible not working as expected


Recommended Posts

Consider that I have the handle ($browser_handle) of a web browser window ('firefox') available in my AutoIt script.

I would like perform some keystrokes after I know that the firefox browser window is open and visible on my display so that I can bring it to focus using -

WinActivate($browser_handle)

or

WinWaitActive($browser_handle)

To make sure that the window is visible before I try to bring it into focus I have a while loop which waits till the state of the window handle is visible (2).

While (Not BitAND(WinGetState($browser_handle), 2)) WEnd //until window visible

If I use a Sleep(5000) function before the while loop then I do not face any issues.

If I do not use an arbitrary Sleep function in my script, the Whileloop condition never becomes true and turns into an infinite loop.

When I tried to check what the return value of WinGetState($browser_handle) is when there is no Sleep function,

It remains 5 even if the browser is visible and becomes 0 after the browser window is closed.

I'm unable to understand why the WinGetStatereturn value never becomes 2(visible) even if the browser is visible when there is no Sleep function.

Note: WinWait does not work in this situation, If you see the example in the link there is a Sleep after WinWait, this function returns even if the window is hidden.

Please let me know how I can get this to work. 

Thanks

Link to comment
Share on other sites

Hi John,

I'm launching my web browser which is driven by java selenium. So that is why it takes some time for the browser to show up.

Any way I was able to reproduce the issue with this code - 

#include <Constants.au3>

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Run($browser_name)

ProcessWait('iexplore.exe')

Local $browsers = ProcessList('iexplore.exe')

Local $pid = $browsers[1][1];

_WinActiveByPID($pid)

Local $sText = WinGetTitle("[ACTIVE]")

; Display the window title.
MsgBox($MB_SYSTEMMODAL, "", $sText)

Func _WinActiveByPID($pid)   ;False to WinActivate, True to just see if it's active
    Local $aWL = WinList()
    For $iCC = 1 To $aWL[0][0]
            If ($aWL[$iCC][0] <> '') And _
                (WinGetProcess($aWL[$iCC][1]) = $pid) Then

                     While (Not BitAND(WinGetState($aWL[$iCC][1]), 2))
                        MsgBox($MB_SYSTEMMODAL, "", WinGetState($aWL[$iCC][1])) ;This is where I see the return value of "5"
                     WEnd
                     WinWait($aWL[$iCC][1])
                     WinActivate($aWL[$iCC][1])
                     WinWaitActive($aWL[$iCC][1])
                     Return 1
            EndIf
     Next
    Return SetError(2, 0, 0)
 EndFunc

Please note that the above issue can be reproduce only if there are no already open IE browsers.

Thanks in advance for your help.

 

Link to comment
Share on other sites

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Local $iPID = Run($browser_name)

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

You are right mLipok what you have suggested will work with respect to the test code I have posted above. 

To elaborate exactly what I'm doing, this is how I start my browser - 

Local $java_PID = ShellExecute('java', '-jar selenium-0.0.1-SNAPSHOT.jar ','jar location')

It is this java program which opens up my web browser, so I cannot get the PID of the browser in the manner you have suggested.

After this I make sure that I wait till the Internet Explorer process starts - 

ProcessWait('iexplore.exe')

From here I get the PID of the browser using the following code and continue - 

Local $browsers = ProcessList('iexplore.exe')

Local $pid = $browsers[1][1];

_WinActiveByPID($pid)

Hope I was able to explain for use case effectively. Thanks for your help.

Link to comment
Share on other sites

Heres a quick effort but not from pid

#include <Constants.au3>
#include <Array.au3>

Opt("WinTitleMatchMode", 2)

; get a list of already open windows
$WinList_1 = WinList('Internet Explorer')

$Wincount = UBound($WinList_1)

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Run($browser_name)

; wait until new window opens
Do
    Sleep(100)
Until UBound(WinList('Internet Explorer')) > $Wincount

; get new window handle
$WinList_2 = WinList('Internet Explorer')
$newwinhwnd = _NewWinHandleFromArrays($WinList_1, $WinList_2)
WinActivate($newwinhwnd)
Local $sText = WinGetTitle($newwinhwnd)

; Display the window title.
MsgBox($MB_SYSTEMMODAL, "", $sText)

WinClose($newwinhwnd)

Func _NewWinHandleFromArrays(ByRef $a1, ByRef $a2)
    For $wl2 = 1 To $a2[0][0]
        $old = 0
        $hwnd = 0
        For $wl1 = 1 To $a1[0][0]
            If $a2[$wl2][1] = $a1[$wl1][1] Then
                $old = 1
            Else
                $hwnd = $a2[$wl2][1]
            EndIf
        Next
        if Not $old Then
            Return $hwnd
        EndIf
    Next
    Return SetError(1)
EndFunc   ;==>_NewWinHandleFromArrays

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Try to use my _IE_RegExpFind()

 

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <array.au3>
#include <ie.au3>
Global Const $IE_NAVOPENINBACKGROUNDTAB = 4096
_IEErrorHandlerRegister(_User_ErrFunc)

_Example()

Func _Example()
;~     Local $oIE = _IECreate('http://www.wp.pl/')
    Local $oIE = _IECreate('https://www.autoitscript.com/')
    $oIE.navigate2('www.google.com',$IE_NAVOPENINBACKGROUNDTAB)
    $oIE.navigate2('https://www.e-sad.gov.pl',$IE_NAVOPENINBACKGROUNDTAB)
    $oIE.navigate2('onet.pl',$IE_NAVOPENINBACKGROUNDTAB)
    $oIE.navigate2('https://crowdin.com/',$IE_NAVOPENINBACKGROUNDTAB)

    _IELoadWait($oIE)
    Sleep(5000)

    Local $aObjects = _IE_RegExpFind('(?is).*?\.pl', 'url', True)
;~     Local $aObjects = _IE_RegExpFind()
    _ArrayDisplay($aObjects, 'Array of $oIE " EXT=' & @extended, '', 0, Default, 'Object Reference|locationName|locationURL|Visible')
EndFunc    ;==>_Example

Func _IE_RegExpFind($sRegExpPattern = Default, $sMode = Default, $bVisiblity = Default)
    Local $oShell = ObjCreate("shell.application")
    Local $oShellWindows = $oShell.windows

    Local $iCount
    If $oShellWindows = Null Then
        Return SetError(1, 0, '')
    Else
        $iCount = $oShellWindows.Count
    EndIf

    Local Enum _
            $ARR_OJBECT, _
            $ARR_NAME, _
            $ARR_URL, _
            $ARR_VISIBLE, _
            $ARR_COUNTER
    Local $aObjects[$iCount][$ARR_COUNTER]

    Local $oIE = Null
    For $iWindow_idx = 0 To $iCount - 1
        $oIE = $oShellWindows.Item($iWindow_idx)
        If @error Then ExitLoop
        $aObjects[$iWindow_idx][$ARR_OJBECT] = $oIE
        $aObjects[$iWindow_idx][$ARR_NAME] = $oIE.LocationName
        $aObjects[$iWindow_idx][$ARR_URL] = $oIE.LocationURL
        $aObjects[$iWindow_idx][$ARR_VISIBLE] = $oIE.Visible
    Next

    ; Clean Up
    $oShellWindows = Null
    $oShell = Null

    If $sRegExpPattern <> Default And $sMode <> Default Then
        Local $iMode = -1
        If $sMode = 'name' Then
            $iMode = $ARR_NAME
        ElseIf $sMode = 'url' Then
            $iMode = $ARR_URL
        EndIf

        For $iWindow_idx = $iCount - 1 To 0 Step -1
            If StringRegExp($aObjects[$iWindow_idx][$iMode], $sRegExpPattern, $STR_REGEXPMATCH) = 0 Then
                _ArrayDelete($aObjects, $iWindow_idx)
                $iCount -= 1
            ElseIf $bVisiblity <> Default Then
                If $bVisiblity <> $aObjects[$iWindow_idx][$ARR_VISIBLE] Then
                    _ArrayDelete($aObjects, $iWindow_idx)
                EndIf
            EndIf
        Next
    EndIf

    Return SetError(0, $iCount, $aObjects)
EndFunc    ;==>_IE_RegExpFind

; User's COM error function. Will be called if COM error occurs
Func _User_ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptDir & '\ie.au3' & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc    ;==>_User_ErrFunc

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Is it that i am no native english speaker, that i can't see the problem? I would solve like this:

#include <Constants.au3>

;Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe "about blank"'
Run($browser_name)

ProcessWait('iexplore.exe')

$hwnd=WinWaitActive('about blank',10)
Local $sText = WinGetTitle($hwnd)

; Display the window title.
MsgBox($MB_SYSTEMMODAL, "Found", $sText)

WinClose($hwnd)

Result: 37_Found.jpg.210e35c2a7cbc6870752734e769

Link to comment
Share on other sites

Do you mean "about:blank"

The following code is my fault - I was incorrect read the OP statement.

Spoiler

Btw. the OP mention that IE opens as HIDEN

#include <Constants.au3>

;Opt("WinTitleMatchMode", 1) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe "about:blank"'
Run($browser_name, "", @SW_HIDE)

ProcessWait('iexplore.exe')

$hwnd=WinWaitActive('about:blank',10)
Local $sText = WinGetTitle($hwnd)

; Display the window title.
MsgBox($MB_SYSTEMMODAL, "Found", $sText)

WinClose($hwnd)

What you can see now ?

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

WindowTitle in IE is not always visible and you can not always relate on this.
This is related to Windows GUI visual settings.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

I made some changes to OP's script:

#include <Constants.au3>

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

Local $browser_name = 'C:\Program Files (x86)\Internet Explorer\iexplore.exe'
Run($browser_name)

ProcessWait('iexplore.exe')
Sleep(500)
Local $browsers = ProcessList('iexplore.exe')
Local $pid = $browsers[1][1], $sText
ConsoleWrite('Started: ' & $pid & @CRLF)
$hwnd = _WinActiveByPID($pid)
If Not @error Then
    WinWaitActive($hwnd)
    $sText = WinGetTitle($hwnd)
    $sPid = WinGetProcess($hwnd)            ;for debuging can be deleted
    ConsoleWrite('Found: ' & $sPid & @CRLF) ;for debuging can be deleted
    ; Display the window title.
    MsgBox($MB_SYSTEMMODAL, "", $sText)
    WinClose($hwnd)
Else
    ConsoleWrite('Error: '&@error&@CRLF)
    ProcessClose($pid)
EndIf
Exit

Func _WinActiveByPID($pid, $iTimeOut = 5000)
    Local $aWL, $hwnd = 0
    Local $dtStart = TimerInit()
    Do
        $aWL = WinList('')
        For $iCC = 1 To $aWL[0][0]
            ;If ($aWL[$iCC][0] <> '') And _
            If      (WinGetProcess($aWL[$iCC][1]) = $pid) Then
                ;#comments-start
                While (Not BitAND(WinGetState($aWL[$iCC][1]), 2))
                    ;MsgBox($MB_SYSTEMMODAL, "", WinGetState($aWL[$iCC][1])) ;This is where I see the return value of "5"
                    Sleep(10)
                WEnd
                ;#comments-end
                $hwnd = WinActivate($aWL[$iCC][1])
                ;WinWaitActive($hwnd)
                $iElapsed = TimerDiff($dtStart)
                ConsoleWrite($hwnd&' activated in '&$iElapsed & ' ms'&@CRLF)
                Return $hwnd
            EndIf
        Next
        Sleep(10)
        $iElapsed = TimerDiff($dtStart)
    Until $iElapsed > $iTimeOut
    ConsoleWrite($hwnd&' :  '&$iElapsed &  ' ms'&@CRLF)
    Return SetError(2,0,0)
EndFunc   ;==>_WinActiveByPID

As

12 hours ago, mLipok said:

WindowTitle in IE is not always visible and you can not always relate on this.
This is related to Windows GUI visual settings.

i check every window.

And it seems to be also a timing problem so i wrapped the workerloop in a do ... until loop which finishes after timeout (5 sec.). The immediately leaving return (after found the hwnd created by the Pid) now returns the hwnd.

Link to comment
Share on other sites

ProcessWait('iexplore.exe')
Sleep(500)
Local $browsers = ProcessList('iexplore.exe')
Local $pid = $browsers[1][1]

The above will not wait for new process, because iexplore.exe might already be running.

It also assumes that the first pid in the list, is the one just started.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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