0

I made an AppleScript to access a webpage and login. Well, at some point I need to click on a button to valid my vote. Here is what I get on that button :

 <form method="post" action="/website/in/16">
 <input type="submit" name="valid" value="Valider mon vote" class="btn btn-default" />

I thought it'd be easy to do it with Javascript, here is my code :

tell application "Safari"
    do JavaScript "document.getElementsByName('valid').click();"
end tell

A click on the link to vote opens a webpage into the existing one, does it change anything for my problem?

1 Answer 1

2

You have to specify the document that the JavaScript should be evaluated in.

do JavaScript "document.readyState" in document 1

Now it's evaluated in the frontmost tab of the frontmost window. A safe way would be something like this:

tell application "Safari"
    do JavaScript "document.getElementsByName('valid').click();" in item 1 of (every tab of every window whose URL starts with "http://www.votepage.com/")
end tell

Now it will work even if the tab is in the background (or Safari is not the frontmost application).

Sign up to request clarification or add additional context in comments.

3 Comments

It appears that you do indeed have to specify a tab, despite Safari's AppleScript dictionary suggesting that the tab spec. is optional (as of OS X 10.9.2).
Thanks for the hint and the correction. I've updated it.
Actually, I was agreeing with you. :) The dictionary states that you specify the target as a tab object, but it appears that you can use a document object as well. However, the dictionary also states that the tab spec is optional, which is not true in reality. Given that the command without a tab spec compiles and fails completely silently, my guess is that it's a bug, and that the intent was to execute in the front window's current tab by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.