Help With A Very Simple Script

Please excuse the fact that I have no real idea of what I am doing here.

I have cobbled together a script, the idea of which is to: open Safari, load a URL, wait for the page to load, and then to click on a button.

The problem is that it only works about once in ten attempts. This is pretty weird because I would at least expect the same result every time.

When it doesn’t work I get the message ‘missing value’ in the results field, although no hint as to what the value might be!

When it does work, it says the following:

button " Sign in
" of group 8 of UI element 1 of scroll area 1 of group 3 of window “Seller Central” of application process “Safari” of application “System Events”

Here is the code. This is running on Safari 5.0.6 and OSX 10.5.8 on a PPC G5. Any help most gratefully received!!

tell application “Safari”
activate

set URL of document 1 to "https://sellercentral-europe.amazon.com/gp/sign-in/logout.html"

delay 0.5
repeat until ((do JavaScript "document.readyState" in document 1) is "complete")
	delay 0.25
end repeat

end tell

activate application “Safari”
tell application “System Events”
tell process “Safari”
click at {329, 287}
end tell
end tell

Amazon probably uses AJAX which means that the page can have an ready state ‘complete’ while the AJAX request is still in progress. Also never click at a location in Safari but click or fireEvent() with JS instead.

Thanks for the reply. Unfortunately you are using terms that I just don’t understand. I literally have a single day’s experience with apple script.

I’m sorry, I’ll try to explain.

document.readyState is a property of a document. When readyState = ‘completed’ it tells you that the DOM (document object model) is loaded. That means that the browser knows how the document looks like. But images, for instance, are loaded after the document’s DOM is loaded. So when a document has an readyState ‘complete’ it doesn’t say it is 100% loaded.

AJAX is a naming for loading data dynamically into my web-page on the fly. Some websites relies fully on AJAX and you can notice that they don’t need any page refreshing. Google’s web mail client for instance uses a lot of AJAX. For many actions no page refresh seems to be needed and the result is that the site seems very user friendly. The reality is that many actions are send and received by the server without page refreshing. All those page loading (in the background) doesn’t affect the readyState as well and remains ‘completed’ every AJAX request.

So now you’re looking for a solution to all this. Well the easiest way is to look if an certain DOM element (like tables, buttons, headings, forms, etc.) exists. When they don’t, just wait.

For instance when a form is loaded with AJAX (which initially doesn’t exists) then a good method could be.

tell application "Safari"
    repeat until ((do JavaScript "document.forms.length" in document 1) - 1 ) = 0
        delay 0.25
    end repeat
end tell

Assuming there are not other scripts running on the page, I like this method of waiting for page loads.

set myEmail to "email"
set myPassword to "password"

tell application "Safari"
	activate
	set URL of document 1 to "https://sellercentral.amazon.com/gp/homepage.html?ie=UTF8&*Version*=1&*entries*=0"
	
	tell me to wait_for_page(1)
	
	do JavaScript "document.getElementById('email').value = " & quoted form of myEmail & "; document.getElementById('password').value = " & quoted form of myPassword & "; document.getElementById('sign-in-button').click()" in document 1
	
	tell me to wait_for_page(1)
	
	
end tell

on wait_for_page(inWindowIndex)
	activate application "Safari"
	tell application "System Events" to tell application process "Safari"
		repeat
			delay 0.5
			set theStatusText to name of static text 1 of group 3 of window inWindowIndex as text
			if theStatusText begins with "Contacting" or theStatusText begins with "Loading" or theStatusText begins with "Waiting" then
			else
				exit repeat
			end if
		end repeat
	end tell
end wait_for_page

Success!!

Thanks for your help guys!

@bazzie wazzie. Your code just seemed to hang, The script paused indefinitely for some reason?

@adayzdone. Your code worked a treat. I flooded my internet connection to slow things to a crawl and everything worked as expected.

I now have to repeat the process for eBay however there is an extra hurdle that I cannot overcome. I need the applescipt to uncheck a checkbox before signing in. I have mastered clicking buttons now using their identifiers but the process isn’t working for the checkbox. Would you tell me how I would uncheck the box “Keep me signed in” on the page https://signin.ebay.co.uk/ws/eBayISAPI.dll?Signin

Thanks again for your help.

It’s an example code, but doesn’t apply to your web page. My explanation was meant to show you how you can tackle this. adayzdone did excactly as I described, wait for an object till it exists then continue. This way you tackle AJAX created objects.

do JavaScript "document.getElementById('signed_in').click()" in document 1