IE - Waiting for element

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
boton
Posts: 8
Joined: 13 Oct 2019, 22:41

IE - Waiting for element

08 Jun 2021, 15:35

Was wondering what would the most efficient way to check for element existing on a webpage. Currently I am loading the webpage and then using the sleep command to wait for the page to fully load so it can run the next line of code without throwing errors.

Code: Select all

sleep, 12000

if (wb.document.getElementsByTagName("H3")[1].InnerText = "Test")
	MsgBox "Element Found"
	
Sometimes this will throw errors since the page will take longer than 12 seconds to load then it will execute the if statement and won't find the element. I was looking around saw maybe I can do the "Loop/Until" ? Or Would the "OnError()" better be suited for this?

Code: Select all

Loop 
	MsgBox "Element Found"
Until wb.dcoument.getElementsByTagName("H3")[1].InnerText = "Test"
gregster
Posts: 9111
Joined: 30 Sep 2013, 06:48

Re: IE - Waiting for element

08 Jun 2021, 15:39

imo try/catch are better suited, in a loop of some kind - until you find something, or a timeout is reached.
boton
Posts: 8
Joined: 13 Oct 2019, 22:41

Re: IE - Waiting for element

08 Jun 2021, 18:28

How would I implement it with a loop? I'm thinking using a while loop and then after finding it exit out of the loop. Would the below implementation work, just wondering with the catch e, are we able to jump back to the code and restart?

Code: Select all

while(elementFound != True){

try(wb.document.getElementsByTagName("H3")[1].InnerText = "Test"){
elementFound := True
 }  catch e {
  
 }
 
 if (elementFound)
 break
 
 ]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 396 guests