[WEB] Click on Link Using XPath [Example]

MACRO:   [WEB] Click on Link Using XPath [Example]

~~~ VER: 2.0    2016-10-12 ~~~

DOWNLOAD:

[WEB] Click on Link Using XPath [Example].kmmacros (11 KB)

Video Demo

See the below post for an example.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Click on the Hyperlink Identified by the XPath
  • Requires either Safari or Google Chrome

MACRO SETUP

This method will work in both Safari and Chrome.

GET THE XPATH
We only need to use Chrome to get the XPath:
(Safari does not have the XPath tool)

  1. Open your web page in Chrome (just one time)
  2. Right-click on the link of interest, and select "Inspect"
  3. This will open the Chrome Developer tool, showing the HTML for the link
  4. Right-click on the HTML for the link, and select "Copy > Copy Xpath"
  5. We are now done with Chrome. You can close it.
  6. In the below Action, "Set KM xPath Variable",
  • Paste in the text block -- this is the xpath. It should look someting like this:
    //*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

POSSIBLE ISSUES WITH XPATH

  1. The "id" used may be dynamically created, and vary within the page, and in similar pages from the same web site. In this case, it may be not be reliable.
  2. If the HTML Element of interest does not have an "id" attribute, the XPath with be a relative path from the nearest element above that does have an "id".
  • If the structure of the web page changes, this path could be broken, causing the XPath to fail.

REF:

USING THE MACRO:

  1. Make sure you have completed the above Setup
  2. Open your Browser to the page of interest
  3. Trigger this macro

TAGS: @XPath @Link @Chrome @Safari @JavaScript @Web @Browser

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Varibles or Clipboards

REQUIRES:
(1) Keyboard Maestro Ver 7.2.1+
(2) Yosemite (10.10.5)+
(3) Either Safari or Google Chrome



JavaScript Used in Macro

(function run (pXPathStr) {
  'use strict';

var ptyScriptName   = "Click on Link Identified by XPath"
var ptyScriptVer     = "2.0"
var ptyScriptDate   = "2016-10-12"
var ptyScriptAuthor = "@JMichaelTX"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Click on the Hyperlink Identified by the XPath provided by KM

RETURNS:  One of these, as text:
  • "OK" -- if successful, no errors
    
  • "[ERROR]" at start of results if a script error occurred.
  
KM VARIABALES REQUIRED:
  • xPath    -- the full XPath to the Link
  
    EXAMPLE XPATH:
    //*[@id="ctl00_MainContent_ucEStatements_dlStatements_ctl00_lbStatementDate"]

KM VARIABLES SET:
  • NONE
  
REF:
  • [Introduction to using XPath in JavaScript](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
  //--- GET THE XPATH FROM KM ---
  var xPathStr = document.kmvar.xPath;
  
  var returnResults = "TBD";
  
  //--- GET THE HTML ELEMENT IDENTIFIED BY THE XPATH ---
  var elemFound =  document.evaluate(
      xPathStr, document, null, 0, null
    ).iterateNext();
    
  if (elemFound) {
    //--- CLICK ON THE LINK ---
    elemFound.click();
    returnResults = "OK";
  }
  else {
    //--- ERROR:  ELEMENT WAS NOT FOUND ---
    returnResults = '[ERROR] Element NOT FOUND for XPath:\n' + xPathStr;
    alert(returnResults);
  }
  
  return returnResults;

})();
4 Likes

Example of Using XPath

Here is an example of using XPath to click on a link for this web page:
https://www.stairways.com/main/download

The XPath is:
//*[@id="DownloadTable"]/tbody/tr[2]/td[4]/a

This will click on the link to the first download:

1 Like

Just added the above video.

Nice, thanks!
But I still can’t process.

The link I wanted to click on has this in Chrome > Inspect

Get Tracking Code

It looks like one of those ajax (same page) webpages.

Please post a screenshot of what you see when the Chrome Inspect window appears, OR, provide the URL to the web page of interest.

Well, it's a Shopify (e-commerce) app called Oberlo

(please see attached)

From your screenshot, it looks like this is a standard HTML Button.
You might be able to "click" it by using the KM Action Submit Google Chrome Form:

With Chrome open to the web page of interest, if you click on the "Google Chrome" drop-down on the Action it will show you a list of forms/buttons to pick from:

If your button does not show up there, we will have to try something different.

Thanks, this works great!
Is it possible to disable the notifications from the macro, so that it does not return ‘OK’ or ‘Error’?
Thanks so much for this!
Eli

Never mind! I edited to remove the Results parts of the macro and now it is no longer verbose.
Thanks a ton! Eli

I find “XPath” completely unreliable, especially when sharing macros. At least in my particular case the XPaths must be dynamic, making them useless. My only other option seems to be “Found Image” clicking which works fine on a single computer, but breaks when installed on any other computer, even though the system and software is identical. It must be related to screen resolution, zoom factor, etc., which is nearly impossible to control over multiple computers.

Bottom line… my experience sharing macros is not going so well.

Actually Xpaths are very reliable, they just must be properly designed.
If you'd like help, please post details, including example HTML code around the link to be clicked, in a new topic.

When I spoke to our developer about them he indicated he was unaware of them. I’ll see what I can share here and try to learn more.

What is "them"?
If you mean XPaths, then see

XPath is very well known in the web dev world.

1 Like

@JMichaelTX thank you for the helpful info. I’ll share this with our developer. Perhaps a few tweaks will make “them” (xPaths) useful within KM.

@JMichaelTX

I seriously can't thank you enough.

Really loving Xpath now.

Only downside is it seems different with each browser (Chrome Profile).

I don't know what you mean by that. Please provide details and real-world examples.

Everytime there's like 2 or 3 chars like this

//*[@id=":4i"]/div/div[2]/span/a

4i

it always seems to be dynamic. Most elements in Gmail are like this.

In that case you need to find an XPath that does "id", or use an XPath that references only the part of the "id" that is constant.

1 Like

//*[@id=":4i"]/div/div[2]/span/a

Could you help, which part is constant here?

Or you gotta check entire page code?

The part of the "id" that does NOT change with each browser.

To be more helpful, I will need the URL, or at least the HTML code for the major section of the page you are trying to use. If you post the HTML, please do so in Code Block.