0

I have managed to put together a script that very nearly works :)

It opens the correct web page in Chrome incognito mode, waits 12 seconds while it loads, navigates to the correct part of the page and opens the voting panel. So far so good.

I can't seem to get JavaScript to click the voting button, despite trying numerous variations such as name etc.

Is there something obviously wrong with the code ?

on run {input, parameters}

do shell script "open -a /Applications/Google\\ Chrome.app --args --incognito"

activate application "Google Chrome"
open location "http://www.schoolgen.co.nz/voteforyourschool/"
delay 12.0

tell application "System Events"
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke tab
    keystroke "ahuroa"
    keystroke tab
    keystroke return
    delay 0.5
end tell
tell application "Google Chrome"
    execute javascript "document.getElementsById( 'votebutton' ).click();"
    delay 0.5
end tell

--tell application "Google Chrome" to quit

end run

2

1 Answer 1

1

Here is how I'd code what you're trying to do and I've tested the following example AppleScript code and it works to achieve the objective.

if running of application "Google Chrome" then tell application "Google Chrome" to quit
delay 1
do shell script "open -a 'Google Chrome' --args --incognito http://www.schoolgen.co.nz/voteforyourschool/"
delay 1
tell application "Google Chrome"
    activate
    --  # Wait until page finishes loading.
    repeat until (loading of active tab of front window is false)
        delay 1
    end repeat
    tell active tab of front window
        --  # Click the "Vote now" button for "Ahuroa School".
        execute javascript "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flexbutton ')[77].click();"
        delay 0.5
        --  # Click the "Vote" button.
        execute javascript "document.getElementsByClassName('votebutton')[0].click();"
        delay 0.5
        --  # Click the "Close" button.
        execute javascript "document.getElementsByClassName('btn btn-default')[0].click();"
        delay 0.5
    end tell
    quit
end tell

I've coded it so if Google Chrome is already open, it's first closed so it can be reopened in Incognito mode, then waits until the page is finished loading, while letting JavaScript handle the rest of what's necessary to achieve the goal, so UI Scripting e.g. "System Events" and keystroke command does not need to be used.

By the way, while most of your code worked there were two points of failure, one being execute javascript was not told where to do so, hence adding tell active tab of front window within the tell application "Google Chrome"block, and instead of using getElementsById, which would not work even with tell active tab of front window, I used getElementsByClassName instead, because it worked.


I've updated my answer again after played around with this a bit more. As presently coded, it doesn't need to search for "ahuroa" and instead just clicks the "Vote now" button for "Ahuroa School", which is "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flex button ')[77]", although that could possibly change. If it does you can either increment or de-increment the number or you can just use the following version of the code which ascertains the appropriate number for use with "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flex button ')[?]".

if running of application "Google Chrome" then tell application "Google Chrome" to quit
delay 1
do shell script "open -a 'Google Chrome' --args --incognito http://www.schoolgen.co.nz/voteforyourschool/"
delay 1
tell application "Google Chrome"
    activate
    --  # Wait until page finishes loading.
    repeat until (loading of active tab of front window is false)
        delay 1
    end repeat
    tell active tab of front window
        --  # Click the "Vote now" button for "Ahuroa School".
        set theCount to execute javascript "document.getElementsByClassName('schoolnameid').length;"
        repeat with i from 0 to theCount
            set theSchoolName to execute javascript "document.getElementsByClassName('schoolnameid')[" & i & "].innerText;"
            if theSchoolName is "Ahuroa School" then
                execute javascript "document.getElementsByClassName('flotediv col-md-3 col-xs-12 col-sm-6 flexbutton ')[" & i & "].click();"
                exit repeat
            end if
        end repeat
        delay 0.5
        --  # Click the "Vote" button.
        execute javascript "document.getElementsByClassName('votebutton')[0].click();"
        delay 0.5
        --  # Click the "Close" button.
        execute javascript "document.getElementsByClassName('btn btn-default')[0].click();"
        delay 0.5
    end tell
    quit
end tell

Note: The example AppleScript code is just that and does not employ any error handling and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

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

1 Comment

Haha ! I knew somebody would take my 'tractor like' code and show me how Lamborghini would have done it! Yours is infinitely more eloquent :) Gosh things have changed since I last coded in BASIC in 1984 :) Thanks so much for your help - much appreciated.

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.