title: AutoHotKey Wait for Internet Explorer to finish loading web page
body: <p>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).</p>
  
  <p>Is there a way to make my script wait for IE to finish loading before proceeding?</p>
  
  <p>I tried the code <code>IELoad(wb)</code> <a href="http://www.autohotkey.com/board/topic/64563-basic-ahk-v11-com-tutorial-for-webpages/" rel="nofollow noreferrer">from here</a>, 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. </p>
  
  <p>Observation: The page will be loaded, but the <code>IELoad(wb)</code> function is still waiting for something.</p>
  
  <pre><code>; 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...
  </code></pre>