Sign in to follow this  
Followers 0
Herb191

Proper way to error check IE

3 posts in this topic

#include <IE.au3>
#Include <Date.au3>

$results = test("Lawnmowers")
MsgBox(0, "", $results)

Func test($kw_to_search_for)
    $shopping_results_on_the_page = "no"

    Do
        $oIE = _IECreate("http://www.google.com", 0, 1)
        check_for_errors(@error)
    Until @error = 0

    Do
        $oForm = _IEFormGetObjByName($oIE, "f")
        check_for_errors(@error)
    Until @error = 0

    Do
        $oQuery = _IEFormElementGetObjByName($oForm, "q")
        check_for_errors(@error)
    Until @error = 0

    Do
        _IEFormElementSetValue($oQuery, $kw_to_search_for) ;enters the KW's that will be searched
        check_for_errors(@error)
    Until @error = 0

    Do
        _IEFormSubmit($oForm, 0)
        check_for_errors(@error)
    Until @error = 0

    Do
        _IELoadWait($oIE)
        check_for_errors(@error)
    Until @error = 0

    ;Checks to see if there are Google shopping results on the page
    $sMyString = "Shopping results for"
    Do
        $oLinks = _IELinkGetCollection($oIE)
        check_for_errors(@error)
    Until @error = 0

    For $oLink In $oLinks
        Do
            $sLinkText = _IEPropertyGet($oLink, "innerText")
            check_for_errors(@error)
        Until @error = 0
        If StringInStr($sLinkText, $sMyString) Then
            $shopping_results_on_the_page = "yes"
            ExitLoop
        EndIf
    Next

    _IEQuit($oIE)

    Return $shopping_results_on_the_page
EndFunc   ;==>google_PR_for_kw

Func check_for_errors($error)
    If $error <> 0 Then
        $file = FileOpen("error.log", 1)

        If $file = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        EndIf

        FileWrite($file, _NowTime() & @CRLF)
        FileWrite($file, $error & @CRLF)
        FileWrite($file, @CRLF)

        FileClose($file)
    EndIf
EndFunc   ;==>check_for_errors

What’s the best way to keep this function from exiting when it gets an error? I basically I want it to keep trying until it works. It works most of the time but when I loop though it with a for loop I occasionally get the following errors:

--> IE.au3 V2.4-0 Error from function _IECreate (Browser Object Creation Failed)

--> IE.au3 V2.4-0 Error from function _IEFormGetObjByName, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IEFormElementGetObjByName, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IEFormElementSetValue, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IEFormSubmit, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IELoadWait, $_IEStatus_InvalidDataType

--> IE.au3 V2.4-0 Error from function _IELinkGetCollection, $_IEStatus_InvalidDataType

C:\Documents and Settings\Owner\Desktop\test\test\test1.au3 (633) : ==> Variable must be of type "Object".:

For $oLink In $oLinks

For $oLink In $oLinks^ ERROR

Share this post


Link to post
Share on other sites



hi herb191,

Search the help file for ObjEvent(), it shows you how to create custom error handlers, interestingly/ironically/luckily, the example uses the IE object.

Hope this helps, :unsure:

-smartee

Share this post


Link to post
Share on other sites

You might also try something like:

#include <IE.au3>
#Include <Date.au3>

$results = test("Lawnmowers")
MsgBox(0, "", $results)

Func test($kw_to_search_for)
   $shopping_results_on_the_page = "No"
   $oIE = _IECreate("http://www.google.com", 0, 1)
   $oForm = _IEFormGetObjByName($oIE, "f")
   $oQuery = _IEFormElementGetObjByName($oForm, "q")
   _IEFormElementSetValue($oQuery, $kw_to_search_for) ;enters the KW's that will be searched
   _IEFormSubmit($oForm, 1)

   ;Checks to see if there are Google shopping results on the page
   $sMyString = "Shopping results for"
   $oLinks = _IELinkGetCollection($oIE)
   If IsObj($oLinks) Then
      For $oLink In $oLinks
         $sLinkText = _IEPropertyGet($oLink, "innerText")
         If StringInStr($sLinkText, $sMyString) Then
            $shopping_results_on_the_page = "Yes"
            ExitLoop
         EndIf
      Next
   Else
      MsgBox(0, "", "Links not found.")
   EndIf

    _IEQuit($oIE)

    Return $shopping_results_on_the_page
EndFunc   ;==>google_PR_for_kw

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