How to click on button or link in a website.
Is it possible to select something specific on a web page? Previously using Keyboard Maestro I've done something like:
- open specific URL
- Wait until finished loading
- Click on link "/html/body/div[6]/div/form/div[1]/div[5]/div/button"
I'd like to avoid "click relative to UI element" as it doesn't seem the most robust method for this
- Christian Scheuer @chrscheuer2021-01-28 22:36:45.725Z
Hi Andrew,
Which browser are you using?
- AAndrew Sherman @Andrew_Sherman
Normally Chrome, but it would be great if the functionality supported the default browser
Christian Scheuer @chrscheuer2021-01-29 11:54:39.138Z
We don't have a cross browser implementation for this (although I agree it would be great).
Here's a script that will enter SoundFlow into the search field of the google front page (www.google.com) and hit Search.
function executeJs(script) { var res = sf.system.execAppleScript({ script: 'tell application "Google Chrome"\n' + 'set str to execute in active tab of the front window JavaScript "' + script.replace(/"/g, '\\"') + '"\n' + 'str\n' + 'end tell\n' }); if (res.standardError) { log(res.standardError); throw 0; } return res.result; } function searchGoogleInOpenTab(query) { executeJs(`document.querySelector('input[role=combobox]').value = ${JSON.stringify(query)}`); executeJs(`document.querySelector('input[type=submit]').click()`); } searchGoogleInOpenTab('SoundFlow');
- AAndrew Sherman @Andrew_Sherman
Thanks, I'll use that as a starting point.
Christian Scheuer @chrscheuer2021-01-29 13:53:24.642Z
Here's one that can also be used with Safari:
function executeJs(appName, script) { var res; if (appName === 'chrome') { res = sf.system.execAppleScript({ script: `tell application "Google Chrome" set str to execute in active tab of the front window JavaScript "${script.replace(/"/g, '\\"')}" str end tell` }); } else if (appName === 'safari') { res = sf.system.execAppleScript({ script: `tell application "Safari" set str to do JavaScript "${script.replace(/"/g, '\\"')}" in document 1 str end tell` }); } else { throw `Unsupported app: ${appName}`; } if (res.standardError) { log(res.standardError); throw 0; } return res.result; } function searchGoogleInOpenTab(query) { var appName = 'safari'; //or 'chrome' executeJs(appName, `document.querySelector('input[role=combobox]').value = ${JSON.stringify(query)}`); executeJs(appName, `document.querySelector('input[type=submit]').click()`); } searchGoogleInOpenTab('SoundFlow');
Please note though, that Safari has more security restrictions, so the user has to do some manual things to allow remote control.