How do I click this HTML-button?

Hi there

I want to make automatic backups of my websites with applescript. From a CSV the password and username should be used to login the backup website and there 3 buttons should been pressed. And this is where I got stuck.

This is (a simple version of) my code:


tell application "Safari"
	open location "http://www.backupserverwebsite.com"
	activate
end tell

delay 2

tell application "System Events"
	keystroke "username"
	keystroke tab
	keystroke "password"
	keystroke return
end tell

delay 2

--===========this is the part that doesn't work:===========--
to clicktagName(thetagName, elementnum)
	tell application "Safari"
		do JavaScript "document.getElementsByTagName(˜" & thetagName & "˜)[" & elementnum & "].click();" in document 1
	end tell
end clicktagName

clicktagName("Tools", 0)
--===================================================--

I have this solution from http://cubemg.com/applescript/how-to-click-a-button-on-a-web-page-with-applescript/ But it’s not working. I tried all the possibilities described on this website and none of them is working.

Here the code of the website:

My problem is, that the button is made as a html-table. How can i navigate to this part?

Another solution I tried was with the class:

--===========this is the part that doesn't work:===========--
to clickClassName(theClassName, elementnum)
	tell application "Safari"
		do JavaScript "document.getElementsByClassName(˜" & theClassName & "˜)[" & elementnum & "].click();" in document 1
	end tell
end clickClassName

clickClassName("titleText", 0)
--=========================================================--

I think this is not working, because also other buttons have the same class.

What am I doing wrong? Do you have a better solution?

Thank you for your help :slight_smile:

My understanding is that your script doesn’t keystroke in the Safari window. Try with this edited version :


tell application "Safari"
	open location "http://www.backupserverwebsite.com"
	activate
end tell

delay 2

tell application "System Events"
	tell process "Safari" # ADDED
		set frontmost to true # ADDED
		keystroke "username"
		keystroke tab
		keystroke "password"
		keystroke return
	end tell # ADDED
end tell

delay 2

clicktagName("Tools", 0) # MOVED

--===========this is the part that doesn't work:===========--
to clicktagName(thetagName, elementnum)
	tell application "Safari"
		do JavaScript "document.getElementsByTagName(˜" & thetagName & "˜)[" & elementnum & "].click();" in document 1
	end tell
end clicktagName

--===================================================--

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) mardi 20 octobre 2015 21:15:41

The login process already worked before. The problem is to click the button on the website.

Moving clicktagName(“Tools”, 0) didn’t help…

Thank you anyway!

I entered the page which you pointed to :
http://cubemg.com/applescript/how-to-cl . plescript/

In it I found a link to a sample web page :
https://newyork.craigslist.org

I copied their sample code inn Script Editor.

--”””””””””””””””””””

to getInputByClass(theClass, num) -- defines a function with two inputs, theClass and num
	tell application "Safari" --tells AS that we are going to use Safari
		set input to do JavaScript "
document.getElementsByClassName(˜" & theClass & "˜)[" & num & "].innerHTML;" in document 1 --- uses JavaScript to set the variable input to the information we want
	end tell
	return input --tells the function to return the value of the variable input
end getInputByClass

to clickClass(theClass, num) --- defines a function with two inputs, theClass and num
	tell application "Safari" -- tells AS that we are going to use Safari
		set input to do JavaScript "
document.getElementsByClassName(˜" & theClass & "˜)[" & num & "].click();" in document 1 --- uses JavaScript to set the variable input to the information we want
	end tell
end clickClass

set x to 0 --- start number
set className to "txt" --- name of class
set searchTerm to "antiques" -- something in the inner HTML

repeat 1000 times --- how many time you want to repeat this
	
	set trialInnerHTML to getInputByClass(className, x) --- pulls content from each class with search term
	log trialInnerHTML
	if searchTerm is in trialInnerHTML then
		clickClass(className, x)
		exit repeat
	end if
	
	set x to x + 1 --- goes to the nextclass
	
end repeat

I ran it and discovered that the getInputByClass handler always returns missing value although the source of the sample web page contain 207 lines containing : class=“txt”

So, it seems clear that something is odd in the script and it would be good practice to ask the original author to double check its proposal.

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) mercredi 21 octobre 2015 15:40:34