2
votes

I am trying to automate status requests to a 3rd party vendor website at work. Currently I make my script wait 40 seconds after each action to wait for a page to load. (load time ranges from 1 second to a few minutes at very worst case).

Is there a way to make my script wait for IE to finish loading before proceeding?

I tried the code IELoad(wb) from here, but it does not always work. (Sometimes it will just sit there, and I have to press F5). Last night I let it ran, and it stopped about 25 cases in.

Observation: The page will be loaded, but the IELoad(wb) function is still waiting for something.

; You need to send the IE handle to the function unless you define it as global.
IELoad(wb) {   
    If !wb    ;If wb is not a valid pointer then quit
        Return False
    Loop    ;Otherwise sleep for .1 seconds untill the page starts loading
        Sleep,100
    Until (wb.busy)
    Loop    ;Once it starts loading wait until completes
        Sleep,100
    Until (!wb.busy)
    Loop    ;optional check to wait for the page to completely load
        Sleep,500
    Until (wb.Document.Readystate = "Complete")
    Return True
}

wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("www.someVendor.com")
IELoad(wb)

; This is the part I have the most problems with
wb.document.getElementById("someLink").click()

; The page might be loaded, but it will sit there and wait forever
IELoad(wb)

; Do some clicking and searching...
2
I've done a lot of browser automation with AHK and your script look ok. You can try to look into the loaded page with wb.Document.Body.innerHTML and do some checks prior to clicking. Personally I moved away from AHK for web automation and now use to free tool Selenium (you may want to google that) which works much better with web sites that have a lot of javascript in them.576i
on second reading, if pressing F5 solves your problem, you could also have a timer running that presses F5 every 10 seconds and activate that timer before the IELoad command and deactivate it afterwards...576i
Instead of performing clicks in a rendered environment you could also try to replicate the HTTP-requests that the buttons perform. It requires a bit of reverse engineering on the HTML and possibly JavaScript, but it will run much more reliable, efficient and smoother. I've created a little guide on how it can be done with AHK, if you're interested: stackoverflow.com/questions/29458900/…Forivin

2 Answers

2
votes

By right clicking the tray icon of your script and pick the option "open" you can see the line that the IEload functions is stuck on, this can tell you what maybe the thing holding up your script.

The IEload function has some issues as it can hang if the browsers jumps ahead faster then the sleeps used within the function and a few other things...

The lines i normally use to let IE load looks like this

While wb.readyState != 4 || wb.document.readyState != "complete" || wb.busy
    Sleep, 10

There are other ways to check the loading state, like using ComObjConnect() and connecting to the DocumentComplete event.

There are also other simpler ways too like checking for the existence of a html Element but that's often only need when working with incomplete pages, frames or elements changed by on page scripts...

1
votes

I've one of my script that once had this problem. I found out that was because their servers weren't allowed to answer to ping requests, so maybe IELoad used them somewhere inside. It will work fine the first time, but not after the first refresh. I had to close the webpage before re-open it instead of only used IE.refresh().