[Chrome.ahk] Check if a button exists Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

[Chrome.ahk] Check if a button exists

21 May 2020, 09:33

Hey,

How can I check if a button exists on a web page, and if exists click on it.
if the button does not exist i get an error message

---------------------------
Error: TypeError: Cannot read property 'click' of undefined
---------------------------

Execute this code only if the button is present.

Code: Select all

PageInst.Evaluate("document.getElementsByClassName('btn-block')[0].click()")
many thanks
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: [Chrome.ahk] Check if a button exists  Topic is solved

21 May 2020, 09:37

I have used try/catch (repeatedly, with a little sleep and a timeout) in similar circumstances with Chrome.ahk.
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Re: [Chrome.ahk] Check if a button exists

21 May 2020, 09:50

thanks, it worked :)

Code: Select all

try {
PageInst.Evaluate("document.getElementsByClassName('btn-block')[0].click()")
} catch e {
 sleep, 500
}
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: [Chrome.ahk] Check if a button exists

21 May 2020, 09:58

I thought of something like this, to have it actually repeat a couple of times, in case it fails.

Code: Select all

count := 0

clickit:
try PageInst.Evaluate("document.getElementsByClassName('btn-block')[0].click()")
catch {
	If (count++ < 5) {	
		sleep, 500
		gosub clickIt
	}
	else
		msgbox Oh no, it timed out!
}
In your code above, the error gets caught, it waits, but that's it then... it doesn't try again and just continues, without having clicked.
Or do you loop it? But where do you break it, in case of success?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 316 guests