Question

Is it possible with AppleScript to choose a clickable element in a web page and click it and if so, can you give me an example?

Was it helpful?

Solution

Yes, generally speaking, it's possible, however it can depend on the browser and the web page, there is no one method fits all, if you will.

Examples:

Using this particular web page, looking at the question title line, to the far right is a blue button with says: Ask Question

The following line of AppleScript code will click that button in Safari if Allow JavaScript from Apple Events is checked on the Safari > Develop menu, which is hidden by default and can be shown by checking [√] Show Develop menu in menu bar in: Safari > Preferences > Advanced

tell application "Safari" to tell document 1 to do JavaScript ¬
    "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"

With Google Chrome the following example AppleScript code does the same thing, without having to modify any of the default settings:

tell application "Google Chrome" to tell active tab in front window to execute javascript ¬
    "document.getElementsByClassName('ws-nowrap s-btn s-btn__primary')[0].click();"

Note that the JavaScript code portion, within the double-quotes, of each of these is the same while the basic AppleScript code is different for how each of the two applications work with the code.

I should also point out that while this is AppleScript, albeit running some JavaScript, it's really the JavaScript that's doing the real work of clicking the target button. Basic vanilla AppleScript without executing some JavaScript cannot really click a button on the web page content unless it's done through the very kludgy use of UI Scripting.

UI Scripting example:

On my system, in macOS High Sierra using Safari, version 11.1.1 (13605.2.8), the following example AppleScript code works to click the button that the other examples click.

tell application "System Events" to click UI element ¬
    "Ask Question" of group 5 of UI element 1 of scroll area 1 of group 1 of ¬
    group 1 of tab group 1 of splitter group 1 of window 1 of application process "Safari"

On my system, in Safari on macOS Catalina you'd have to change of group 5 of UI element 1 to of group 6 of UI element 1, and while that seems minor, nonetheless, it will depend on what you're trying to click and where and some objects on a web page may not be clickable at all with this method, yet will most likely be doable with JavaScript calls.

This wouldn't work in Google Chrome as it does not expose that particular UI element to click in this manner.

One of the issues with UI Scripting is the hierarchical structure to the object is subject to change between versions of the application or the versions of macOS and would need to be modified, where the use of AppleScript with JavaScript will typically continue to work without modification.

By the way, none of this is doable in Firefox.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top