Need help with AppleScript to click a button of a webpage

Dear all,

I would appreciate your help on following automation I’m trying to set up:

  • When an email with a job offer comes in, it executes a rule that triggers an AppleScript. This works fine.
  • The AppleScript retrieves a weblink with the details of the offer from the email. This works fine.
  • The AppleScript opens the weblink. This works fine.
  • But now I need to automate the button-click to “accept” the offer. And this is where I need some help. My knowledge op Applescript does not reach far enough to make that last bit to work. I have already tried numerous things by using the “type” or “class” or “name” of the button, but can’t get it working. But probably I’m not understanding the source code of the webpage (the webpage is also “dynamic”. Meaning once the offer was excepted by someone else, it disappears)

Who can help me with that? I’m running MacOS Ventura 13.1. I have saved the webpage, so I can always share the source.

Many thanks

Ronny
(from Belgium)

Hello Ronny,
There are a couple different ways to do this depending on the properties of the button you’re trying to click, but I’ve supplied one example below. This assumes the target button doesn’t have a ‘Name’ or an ‘ID’, but instead uses the ‘Value’ of the submit button in order to click it. In the example case, the ‘Value’ of the button is ‘Submit’.

tell application "Safari"
	activate
	set theScript to "document.querySelector('input[value=\"Submit\"]').click();"
	do JavaScript theScript in current tab of first window
end tell

One more thing I forgot to mention. You MUST select “Allow JavaScript from Apple Events” in the “Developer” menu of Safari in order to make this work. In order to see the “Developer” menu in Safari, enter the Safari Preferences and go to the “Advanced” tab. At the bottom of that window, you’ll see an option “Show Develop menu in menu bar”, you’ll want to check that option.

I believe given that example page, you’d be better off using something like this (this is untested on my end since I don’t have access to that site)

		tell application "Safari"
			activate
			set theScript to "document.querySelector(\"#edit-submit\").click();"
			do JavaScript theScript in current tab of first window
		end tell

I have included you code into my subroutine. In order to test it, I must find a way that safari opens the offline webpage on my desktop. Any idea how to do that?

set theFile to "Macintosh HD:Users:yourAccountName:Desktop:demo.html"
openLocation(theFile)


on openLocation(localFilePath)
	tell application "Safari"
		activate
		open file localFilePath
		delay 2 # GIVE TIME FOR THE FILE TO LOAD IN SAFARI
		set theScript to "document.getElementById(\"edit-submit\").click();"
		do JavaScript theScript in current tab of first window
	end tell
	-- do shell script "afplay-v 2 '/Users/ronnysuetens/Sounds/alarm.aiff'
	stop
end openLocation

Have a look at this forum thread, and the web pages linked to.

Clicking a button can be done by name, class, id or index, if I recall.

Choose the one that works best for you.

Scripting Safari for Basic Control with AppleScript and Javascript - AppleScript - Late Night Software Ltd.

Sample Buttons And Inputs – CubeMG

How to click a button on a Web Page with AppleScript – CubeMG

Dear Shai1, this seems to work on the offline webpage. Very confident it will also work when the email with the dynamic webpage comes in.

MANY MANY THANKS for your help. Really appreciated.

Hi,

I’m now getting the below error-message when auto-clicking te webpage. I had JavaScript enabled before, so I don’t understand where this is getting from.

“You have reached this page because you submitted a form that required JavaScript to be enabled on your browser. This protection is in place to attempt to prevent automated submissions made on forms. Please return to the page that you came from and enable JavaScript on your browser before attempting to submit the form again.”

Is it possible the webpage-owner has blocked this?

Br
Ronny