Applescript only does its job fully on second time (entering webmail)

For my emails, the organization I found best is having one central (but more private) mailbox combined with a dozen more specialized mailboxes. Any mail to a secondary mailbox automatically gets copied to the central mailbox, so I only need to check my central mailbox.

However I doneed to enter the secondary mailboxes once in a while lest they be considered as bogus accounts and deleted. So, a script to just enter/exit each of them in turn would be quite useful to me.

Below is a script supposed to enter one mailbox. It does its job but only the second trial (the first time it only goes to the mailbox page without entering it). With this type of behaviour I can’t combine exiting of one mailbox with entering another.

What’s going on here ? Perhaps I should insert delays into the code ?

to goToWebPage(theWebPage)
	tell application "Safari"
		activate
		set URL of document 1 to theWebPage
	end tell
end goToWebPage

to clickID(theId)
	tell application "Safari"
		
		do JavaScript "document.getElementById('" & theId & "').click();" in document 1
		
	end tell
	
end clickID

to inputByID(theId, theValue)
	
	tell application "Safari"
		
		do JavaScript "  document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
	end tell
	
end inputByID

to clickClassName(theClassName, elementnum)
	
	tell application "Safari"
		
		do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
		
	end tell
	
end clickClassName


goToWebPage("https://gmx.com")
clickID("login-button")
inputByID("login-email", "************************")
inputByID("login-password", "************************")
clickClassName("login-submit", 0)

You must sync your script’s actions with Safari’s actions. For example, like this:


on waitSafariWebPageLoading() -- ADDED
	tell application "System Events" to tell application process "Safari"
		repeat until ((UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists) or (UI element "Reload this page" of group 2 of toolbar 1 of window 1 exists))
			delay 0.1
		end repeat
	end tell
end waitSafariWebPageLoading

to goToWebPage(theWebPage)
	tell application "Safari"
		activate
		open location theWebPage
	end tell
end goToWebPage

on clickID(theId)
	tell application "Safari" to do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end clickID

on inputByID(theId, theValue)
	tell application "Safari" to do JavaScript " document.getElementById('" & theId & "').value ='" & theValue & "';" in document 1
end inputByID

on clickClassName(theClassName, elementnum)
	tell application "Safari" to do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end clickClassName


goToWebPage("https://gmx.com")
waitSafariWebPageLoading() -- ADDED
clickID("login-button")
delay 1 -- ADDED
inputByID("login-email", "************************")
inputByID("login-password", "************************")
clickClassName("login-submit", 0)

Worked for me, thanks. Now I’m encountering another kind of problem, I’ll ask a separate question about it.