Keyboard Maestro has excellent built-in actions for clicking on web-page links specified by their labels.
For cases where the target is not uniquely labelled, here is a custom action (generalised from an earlier macro ) which should be able to identify and click on any item on a web page.
The item is specified by an XPath, applied either to the whole document, or to the current selection, or mouse position.
Moderator Edit -- 2017-06-26 3:39 PM CT
Original plug in no longer works. The author has issued an update in a later post in this thread:
Custom Keyboard Maestro Plug-in
NAME
Click Chrome or Safari item at XPath
VERSION
0.1
SYNOPSIS
Clicks a button, link or other item on a Chrome or Safari web page.
The item is specified by an XPath which can be applied to:
Note 1: an XPath applied to a mouse position or selection should start with a dot ./, referring to the selected or hovered node
Note 2: In Google Chrome, a simple absolute XPath for a selected element can be obtained with:
- Right-Click, Inspect Element, then
- Right-Click, Copy XPath
( The copied XPath will work in Safari, as well as in Chrome itself – Safari does not have its own Copy XPath feature )
Applied to:
document
mouse position
selection
REQUIREMENTS
Yosemite
The core script clickItemAtXPath.sh is mainly written in Javascript for Applications
INSTALLATION
Drag the .zip file onto the Keyboard Maestro icon in the OS X toolbar.
(if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
Unlike @carycrusiau, I wasn’t able to get this to work at all. As a test I used the example in the previous macro to see if I could even get it to click the search box on the page & no luck. Nothing happens in either Chrome or Safari.
What else can I provide to get help in getting this to work?
Thanks for this custom action and the macro that proceeded it.
Can you confirm whether this action is all that is needed to “click” the item represented by the XPath (the macro needed an If/then statement to execute the XPath)? I have the following but can’t get it to click that item. The XPath comes from using Chrome’s Inspect; the same XPath works successfully in the macro.
XPath: //*[@id=“downloadDocumentLink0”]
applied to: document
Browser: Google Chrome
Also, Click Google Chrome item matching XPath doesn’t appear to accept a variable, so is it possible use a loop to click on multiple similar XPath items (download links to bank statements) such as:
/[@id=“downloadDocumentLink0”]
/[@id=“downloadDocumentLink1”]
/*[@id=“downloadDocumentLink2”]
etc.
I no longer have editing rights to the original post, alas, but I have now tracked down the problem (a shift in the optional status of the Results key), and this copy now works again on my system:
After a few hours of despair, I think I found the reasons, why so many people - me included - seem to struggle with this KM action. Most examples of XPath syntax use double quotes, e.g.:
//*[@id="search-button"]
However this does not work in the KM custom action. Because of the scripts it uses, it can only parse single quotes, so you need to write instead:
//*[@id='search-button']
While searching for a solution, I found out that you can test and debug your XPath definition from the console within the Google Chrome developer tools. Just type:
$x('<your_xpath_definition>')
But be aware that you need to use double quotes within your XPath there. So it becomes:
$x('//*[@id="search-button"]')
When your syntax is correct the console returns the full line of HTML you were searching for and when you hover the mouse pointer over the result, the actual element is also highlighted in the content area. From there you can just copy it into KM. But don’t forget to change the quote symbols back to single quotes
Hope this will save people some time. And by the way: A big thank you to @ComplexPoint for this great custom action. It is very helpful
Unfortunately I have no idea. I even gave it a try. My XPath syntax worked from the devel console from within Safari, but not when triggered from KM. So my guess is, that there is a problem with the custom action, that affects Safari only. Sorry. Maybe @ComplexPoint could have another look at it.