Click on the first web page item that matches an XPath

There have been couple of recent posts asking about using KM to click on particular elements on web pages.


Here is a generic macro which clicks on the first item (in Safari or Chrome) which matches an XPath expression in a KM variable.

(with the path //*[@id="search-button"] for example, it will just click on the Search button on this page.

Click the first Web element that matches an XPath.kmmacros (4.8 KB)

XPath queries work in Safari and Chrome.
One way of getting the XPath to an element (for use in either browser) is to use Chrome's Copy As Xpath command:

  1. Ctrl-click the clickable element of interest in Chrome
  2. Choose Inspect Element
  3. Select the relevant HTML element (typically an <a> element)
  4. Ctrl-click again and choose Copy as XPath
  5. Paste into the value field of the macro's Set Variable action

The .js code in the Execute JavaScript in Safari|Chrome actions is the same:

It:

  • Retrieves the kmvar.xPath variable from the browser document object
  • assumes that its value is a tested XPath expression, and passes it to the document.evaluate() function
  • clicks the first matching element on the front page in the browser
(function (strPath) {
    (function (e) {
        if (e.fireEvent) e.fireEvent('onclick');
        else {
            var o = document.createEvent('Events');
            o.initEvent('click', true, false);
            e.dispatchEvent(o);
        }
    })(
        document.evaluate(
            strPath, document, null, 0, null
        ).iterateNext()
    );
})(document.kmvar.xPath);
3 Likes

Thanks for this. Big help.

Now generalised as a custom plugin at:

1 Like

Thanks for sharing this approach. It definitely helped put me on the right track to using XPaths.

Unfortunately, your JavaScript doesn't work properly for me when used to click on the "cancel" link to a KM Forum reply or new topic:

  • It does click on the link
  • But then it causes the popup button "Yes, abandon" to be closed without clicking.

Here is the XPath that I used:
//*[@id="reply-control"]/div[2]/div[3]/div/div[3]/a

However, I found this simple script to work very well:

clickOnLink(document.kmvar.xPath);

function clickOnLink (pXPathStr) {

  var elemFound =  document.evaluate(
      pXPathStr, document, null, 0, null
    ).iterateNext();
    
  if (elemFound) {
    elemFound.click();
  }
  else {
    alert('Element NOT FOUND for XPath:\n' + pXPathStr);
  }

};

I don't really understand the purpose of the function(e) in your script, but I don't seem to need it.

1 Like

5 posts were split to a new topic: How Do I Automate Fill of GMail Compose Form?

Thanks @ComplexPoint, I worked with JMichaelTX on an xPath statement and couldn’t get it to work. Reading through all the posts I tried your JavaScript and low and behold it works. Finally got one of the last three remaining “Statement Fetchers” to work.

This works beautifully.

2 Likes

Would anyone know if there Is a way to adapt this function to instead click on the FIRST xpath found, to be able to click on the nth occurrence of the xpath

I ask because a web page I am trying to automate uses the same xpath for all the buttons, so it would be ideal to be able to say "click on the 5th occurence of the xpath"

I prefer using querySelector to XPath.

Here is an example of clicking on the nth link:

From: @Web Add Number to Search Link and Click by Number @Example