Applescript to click on a webpage link

I'm looking for a way to automate a click on a link in a web page. Basically, I need to click disconnect on a modem control page and then 30 seconds later, click on the same button (that changes to the name 'connect') so that the modem is reset.

Posted on Dec 15, 2012 6:35 PM

Reply
6 replies

Dec 15, 2012 10:00 PM in response to chmed

The simple answer to this is to use JavaScript, which is the de facto scripting nature of the web.


Examine the source code of the page in question to find the name of the web form the button is associated with, or maybe even the button itself.


If the button is the submit button (looking at the source code will tell you), then you can emulate submitting the form:


tell application "Safari"

do JavaScript "document.forms[\"myform\"].submit();" in window 1

end tell

(the above assumes the form has a name/ID of 'myform').


If it's just a button not associated with a form, you can click it via:


tell application "Safari"

do JavaScript "document.getElementById(\"myButtonId\").click();" in window 1

end tell

Where 'myButtonId' is the name of the button in question (not necessarily its label)

Aug 10, 2014 2:46 AM in response to kinetic125

You'd need to iterate through the elements to get the button to be clicked. Like this:



tell application "Safari"
    do JavaScript "
function main()
{
    var ee = document.getElementsByTagName('input');
    for (var i = 0; i < ee.length; ++i) {
        var e = ee.item(i);
        if (e.getAttribute('class') == 'button' && e.getAttribute('value') == 'search jobs')
        {
            e.click();
            return true;
        }
    }
    return false;
}
main();" in window 1's current tab
end tell


Regards,

H


PS. Next time, please start new thread for your own question. 🙂

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to click on a webpage link

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.