Click item on Chrome or Safari page by XPath

For reference, the unminified .js

// Rob Trew Twitter @ComplexPoint 2015 MIT License 
(function (strPath, strScope, strBrowser) {

  function fnClick(strPath, strScope) {
    var oAnchor = (strScope === 'document') ?
      document : (
        strScope === 'selection' ?
        window.getSelection().anchorNode : null

      nh = oAnchor ? null : document.querySelectorAll(':hover'),
      iLast = (nh ? nh.length : null),
      nodeHover = iLast ? nh[iLast - 1] : null,

      oRoot = oAnchor ? oAnchor : nodeHover,
      nodeToClick = oRoot ? document.evaluate(
      ).singleNodeValue : null;

    if (nodeToClick) {
      if (nodeToClick.fireEvent) {
      } else {
        var e = document.createEvent("Events");
        e.initEvent("click", true, false);
      return 0;
    } else return 1;

  // Evaluate code for a function application to a named browser (Chrome | Safari)
  // fn --> [arg] --> strBrowserName --> a
  function evalJSinBrowser(fnMain, lstArgs, strBrowser) {

    var strDefault = "Safari",
      blnSafari = ((
        strBrowser = ((strBrowser || '').indexOf(strDefault) === 0) ?
        strDefault : "Google Chrome"
      ) === strDefault),
      appBrowser = Application(strBrowser),
      lstWins =,
      lngWins = lstWins.length,

      // an open window (new if none exists)
      oWin = lngWins ? lstWins[0] : blnSafari ?
      appBrowser.Document().make() &&[0] :

      // code of an fnMain().apply(null, lstArgs) function application
      strJS = [
        ').apply(null, ',

    return (
      blnSafari ?
        strJS, {
          "in": oWin.tabs[0]
      ) :
        "javascript": strJS

  /***** MAIN ***/
  evalJSinBrowser(fnClick, [strPath, strScope], strBrowser);


Thanks, Rob. I think this will be very useful! :+1:

###For those not familiar with XPath, like me, you may find these helpful:

  1. Introduction to using XPath in JavaScript @
  2. XPath Tutorial @
1 Like

What am I doing wrong? I cannot get it to work! The previous macro works great but the plugin does not work :frowning:

What are you doing ?

probably best to:

  1. post a macro
  2. tell us what you expect it to do
  3. show us what is happening instead

Forget it! I restarted Keyboard Maestro and it works fine now…

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?


Same checklist as Sep 15 (above), I think.

the search box on the page

The problem, incidentally could be with the two definite articles there : - )

( No two HTML pages or search boxes necessarily have the same (or even similar) XPaths )

I just tried it by copying the XPath from the search element on this page and still nothing happens. :confused:

If you post a macro we can look at it.

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:




I tried this macro and couldn’t get it to work. Here’s a short screencast showing what I did:

Can you tell me what I’m doing wrong? Thanks.

Hey Dan,

I think that plugin is currently broken.


Well, dang! LOL. Thanks for the reply.

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:

Click Chrome or Safari item at (8.4 KB)

1 Like

Had to watch my video to remember what I was trying to do!

Thanks for fixing it.

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.:


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:


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:


But be aware that you need to use double quotes within your XPath there. So it becomes:


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 :slight_smile:

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



Well done Julian!

any idea why it works in Chrome but not in Safari ?

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.

I am not sure what I am missing but I can’t import this macro to KM. I unzip it and get some files but how do I get it to work within KM? :expressionless: