1

I am trying to automate a javascript function call on an external webpage.

the line of code I want to execute is triggered by the click of this div

<div class="vote_btn 151" onclick="vote('151', 'California Square Red Paso','145');">&nbsp;VOTE ►</div>

Instead of automating the clicking of the div, I am just skipping to calling the vote function. I am attempting to do this in two browsers. Here is the applescript for safari:

tell application "Safari" to do JavaScript "vote('151', 'California Square Red Paso', '145');" in document 1

and for chrome:

tell application "Google Chrome"

    tell window 1

        tell tab 1

            execute javascript "vote('151', 'California Square Red Paso', '145');"

        end tell

    end tell

end tell

the problem is, I am getting weird errors. For chrome, it says "vote is not defined" and safari "variable vote is not defined". It seems like my applescripts aren't able to access the javascript? I know there are similar questions to this on stackoverflow but I have read and tried all of the solutions from them with no successful results. Any help will be greatly appreciated!

Alek Hurst
  • 4,527
  • 5
  • 19
  • 24
  • What happens if you type the JavaScript line in the browser bar? That is, what happens if you type "javascript:vote('151', 'California Square Red Paso', '145');" in the URL bar? What shows up in the error console? – Jerry Stratton Apr 06 '14 at 18:17
  • disregard the last response, it is now giving me the same error in safari "can't find variable vote" – Alek Hurst Apr 06 '14 at 18:29
  • It sounds like the browser itself can’t access the function, at least within the scope that AppleScript has access to. If you can’t do it from the browser bar, I don’t think AppleScript can do it either. Is it possible that the
    example you have above is part of an iframe? If so, you would need to access it via that frame. (See http://stackoverflow.com/questions/251420/invoking-javascript-code-in-an-iframe-from-the-parent-page if that’s the case.)
    – Jerry Stratton Apr 07 '14 at 00:24

0 Answers0