Jump to content

Recommended Posts

Posted
#include <IE.au3>

Local $oIE = _IECreate("http://www.autoitscript.com")

For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & isObj($oIE) & @CRLF)
Next

Do
  ProcessClose("iexplore.exe")
  Sleep(100)
Until (Not ProcessExists("iexplore.exe"))

Sleep(2000)
ConsoleWrite(@CRLF & "ProcessCloseed...." & @CRLF & @CRLF)

For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & isObj($oIE) & @CRLF)
Next

MsgBox(4096,"url",$oIE.document.parentWindow.location.href)

As in the above example, when open the IE browser,  the object variable $oIE is obtained.

But when the browser is shut down, the $oIE  still exists as  the object variable.

This causes unpredictable errors during the script running and even causes the script to crash.

Using ObjEvent("AutoIt.Error")  capture errors but can cause the script to crash sometime.

Are there any other ways to detect absent or unavailable object variables?

thanks!

Posted
  On 2/12/2022 at 6:07 PM, Letraindusoir said:

Using ObjEvent("AutoIt.Error")  capture errors but can cause the script to crash sometime.

Expand  

I would like to see a script that causes AutoIt to crash because of the use of ObjEvent.  I am using it all the time, and never seen a crash caused by it.  Really would love to see a replicable script...

Posted

This works for me: 

#include <IE.au3>

Global $oIE = _IECreate("http://www.autoitscript.com")
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrorFunc")
For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & IsObj($oIE) & @CRLF)
Next
Do
    ProcessClose("iexplore.exe")
    Sleep(100)
Until (Not ProcessExists("iexplore.exe"))
Sleep(2000)
ConsoleWrite(@CRLF & "ProcessClosed...." & @CRLF & @CRLF)
For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & IsObj($oIE) & @CRLF)
Next
Global $sURL = $oIE.document.parentWindow.location.href ; COM Error handler does not return the error raised by document but by href. Does value does not explain the real problem!
if @error = 0x800706BA Then Exit MsgBox(0, "Error #1", "RPC-Server is no longer available - means: IE has been closed by user!")
Global $oDocument = $oIE.document
if @error = 0x800706BA Then Exit MsgBox(0, "Error #2", "RPC-Server is no longer available - means: IE has been closed by user!")

Func _ErrorFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $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   ;==>_ErrFunc

You will notice that you only get message "Error #2". But you get 2 COM errors written to the console with the same error code.
The problem is caused by AutoIt having problems with nested COM operations where more than 1 operation fails. This could cause the crashes you encounter!

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

The only solution to this problem is to avoid nesting:

#include <IE.au3>

Global $oIE = _IECreate("http://www.autoitscript.com")
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrorFunc")
For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & IsObj($oIE) & @CRLF)
Next
Do
    ProcessClose("iexplore.exe")
    Sleep(100)
Until (Not ProcessExists("iexplore.exe"))
Sleep(2000)
ConsoleWrite(@CRLF & "ProcessClosed...." & @CRLF & @CRLF)
For $i = 0 To 20
    ConsoleWrite("isObj($oIE) = " & IsObj($oIE) & @CRLF)
Next
; Global $sURL = $oIE.document.parentWindow.location.href ; COM Error handler does not return the error raised by document but by href. Does value does not explain the real problem!
Global $oDocument = $oIE.document
if @error = 0x800706BA Then Exit MsgBox(0, "Error #1", "RPC-Server is no longer available - means: IE has been closed by user!")
Global $sURL = $oDocument.parentWindow.location.href

Func _ErrorFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $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   ;==>_ErrFunc

As IE is no longer available, we know that the error occurs after the first operation.
If the first operation is successful we can then use nested oprations.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

I think you misunderstood the OPs problem.
When IE is closed while the script is still running it will crash the next time it tries to access the IE object.
 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)

 

  On 2/12/2022 at 6:07 PM, Letraindusoir said:

Using ObjEvent("AutoIt.Error")  capture errors but can cause the script to crash sometime.

Expand  

it was fixed:

https://www.autoitscript.com/trac/autoit/ticket/3167

and is in recent beta:

There is workaround, you should split this:

$oIE.document.parentWindow.location.href

  into:

Local $oCOMErrorHandler = ObjEvent("AutoIt.Error") 
 
Local $oDocument = $oIE.document
if @error .....

Local $oWindow = $oDocument.parentWindow
if @error .....

Local $oLocation = $oWindow.location
if @error .....

Local $sHREF = $oLocation.href
if @error .....

 

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:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 2/12/2022 at 10:55 PM, mLipok said:

There is workaround, you should split this:

$oIE.document.parentWindow.location.href

  into:

Local $oCOMErrorHandler = ObjEvent("AutoIt.Error") 
 
Local $oDocument = $oIE.document
if @error .....

Local $oWindow = $oDocument.parentWindow
if @error .....

Local $oLocation = $oWindow.location
if @error .....

Local $sHREF = $oLocation.href
if @error .....
Expand  

Exactly what I posted here ;)

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

ok what @water said, is the right way. ...also, and just for the record... if you don't want to use something like ObjEvent ("AutoIt.Error", ...) there is also another "quick and dirty" workaround to avoid crashes that I've seen used by @genius257 here: https://www.autoitscript.com/forum/topic/185387-value-of-js-variable-in-ie/?do=findComment&comment=1332372 and explained here: https://www.autoitscript.com/forum/topic/185387-value-of-js-variable-in-ie/?do=findComment&comment=1332585

in short you could wrap your "$oIE.document.parentWindow.location.href" (or whatever) in an Execute() statement and check for errors right after the call, that way you can see if something went wrong without a "crash", something like this:

$vResult = Execute('$oIE.document.parentWindow.location.href')
If @error Then Exit MsgBox(0, 'sorry', '...something went wrong.')

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

Native american - when you discover you are riding a dead horse the best strategy is to dismount

Means: Stop automating IE, move to Chrome, Edge or Firefox and use the WebDriver UDF to automate this browsers :)

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 2/13/2022 at 10:45 AM, water said:

... Means: Stop automating IE, move to Chrome, Edge or Firefox and use the WebDriver UDF to automate this browsers :)

Expand  

I agree, however IE can still be very useful in terms of being "embedded" in the GUIs, which is not so simple (so far) for other browsers

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted (edited)

Thank every master for advice!

Capturing the error with 'ObjEvent' seems to be not accurate enough and I have used it for the debugging stage before.

Capture error reprocessing (try....Catch.....) ,I don't know how to use it, so often causing the script to be interrupted or not robust enough.

#include <IE.au3>

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrorFunc")
Global $oDocument = 0
Global $oIE = 0, $sURL = ""

$oIE = _IECreate("https://www.autoitscript.com/site/", 1,1,1,1)
If Not IsObj($oIE) Then Exit

Local $oSlider = 0
Local $bClicked = False
Local $n = 0
While 1
    If $n > 50 Then ExitLoop
    If Not IsObj($oIE) Then $oIE = _IECreate("https://www.autoitscript.com/site/", 1,1,1,1)
    $oDocument = $oIE.document
    If @error = 0x800706BA Then Exit MsgBox(0, "Error #1", "RPC-Server is no longer available - means: IE has been closed by user!")
    $sURL = $oDocument.parentWindow.location.href
    ConsoleWrite("src = " & $sURL & @CRLF)
    
    If ($bClicked = False) Then
        _IENavigate($oIE, "https://www.autoitscript.com/forum/")
        $oSlider = _IEGetObjById($oIE, "ukpt_slider")
        If IsObj($oSlider) Then
            $oSlider.click()
            $bClicked = True
        EndIf
    EndIf
    $n += 1
WEnd

Func _ErrorFunc($oError)
    ConsoleWrite(@ScriptName & " (" & $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   ;==>_ErrorFunc

If Not  IsObj($oIE) Then $oIE = _IECreate("https://www.autoitscript.com/site/", 1,1,1,1)

if find that the "$oIE"  object is no longer existing or unusable(For either unforeseen or unknown reasons), then _IECreate it again...

Edited by Letraindusoir

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
  • Recently Browsing   0 members

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