Click command works in Javascript console but not in Applescript

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 do need 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 and exit it. When I try it, everything works according to plan except for the last click-to-logout command which isn’t executed. Yet, if I execute the command in the browser’s Javascript console, it works. How can I fix this ?

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

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 clickLogoutButtonInGmx()
	
	tell application "Safari"
		
		do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0].click();" in document 1
		
	end tell
	
end clickLogoutButtonInGmx


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


 

Hi,

Login manually in your account of this site. Then, run following and tell us if it worked:


tell application "Safari"
       
       do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0].click();" in document 1
       
end tell

I just tried it several times, it works.

Well,

I asked to perform this test in order to make sure that after login, you need to maintain a certain pause before the log out command.

You do this using a check for completeness of the page load, but in this case it will not work, since the page has already been loaded a long time ago. I recommend instead checking for the existence of the “Log out” button before the script attempts to click it. Or, simply put some delay to give time webpage’s button “Log in” to change to “Log out”.

Following is not tested, it only shows the idea:


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

repeat
	tell application "Safari"
		do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0]" in document 1
		if not (result is missing value) then
			do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0].click();" in document 1
			exit repeat
		end if
	end tell
end repeat

Yes, after a little bit of trial and error a delay of 1.5 s seems to guarantee success every time (it’s less rigorous that your other solution though).

Applescript complains that variable “result” is not defined.

I tried

set result to do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0]" in document 1 

but that is also incorrect.

The question is, how do I store the result of a “do” in an Applescript variable ?

I have not user account on your site, so I can’t determine what class returns the command above (to coerce it to equivalent AppleScript class). I will show you other approach (example) to check the existence (by tag name):


set TextTeg to do JavaScript "document.getElementsByTagName('span')[67].innerHTML"
set googleTranslation to TextTeg as text -- because I know: tag is some JavaScript string

Got it. The innerHTML works on pretty much anything.

The source for the clickable object is

Hi, again.

I think, one simple way to logout correctly should be checking existence of “Logout” button in the HTML of page. Still not have account to test myself:


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

tell application "Safari"
	repeat while (get text of document 1) contains "Logout"
		do JavaScript "document.querySelectorAll(\"[icon='core_logout']\")[0].click();" in document 1
	end repeat
end tell

It similar to getting innerHTML of certain element as text. Only, innerHTML is better because it decrements searching word “Logout” at certain place of whole HTML. You determine first what element is parent of “Logout” button, then you get its inner HTML, coerce it to text, then check if this text contains word “Logout”. This way searching is specific to certain webpage, as you understand.