Find and click a link?

I want to automate the clicking of a link on a webpage. The trick is, the location of the link is always in a different location, however it is always named the same. Is there a way to 1)FIND the link (based on name or text) and then 2)CLICK it?

Thanks much folks!!

1 Like

I think I’m probably missing a subtlety there.

If it is always the same link, then, of course you would just need a macro which goes to that site.

Do you perhaps mean that the link text is always the same, but the underlying hyperlink varies ?

That is correct. For example the link title is “Download” but the file url is always different.

Certainly possible, tho it can, of course, be dangerous to follow a link simply on the basis of its label text, without checking its underlying href.

If you were going to do that, on safe pages that you know well, you could:

  1. use a (Safari or Chrome) 'Execute Javascript in ...' action to harvest links which match an xpath expression
  2. capture the resulting line(s) in a KM variable
  3. Use an 'Open URL' action (embedded in a foreach in case of multiple lines)

The Javascript (this version to capture a single link) might look something like this:

(function (strPath) {
    var r = document.evaluate(strPath, document, null, 0, null),
      lst = [],

    while (oNode = r.iterateNext()) {
        return lst[0];
})("//a[text()='" + document.kmvar.linkText + "']")


Thank you very much for taking the time to spell all this out. that is very helpful to me.

I’ll dig into this and report back.

I may be missing something here, but wouldn’t it be simpler to use the “Click Safari Link” action?

Yes – the click [ Chrome | Safari ] link (by label) actions are excellent and a lot simpler – using an XPath just has a little more reach and power for the edge cases.

The kind of thing that comes to mind is:

  • multiple links with the same legend – several links labelled 'Download' on one page for example
  • links with a graphic but no text
  • links with mutable or unpredicable text etc

( When the label is an elusive anchor you can often still reach for an XPath and specify a link by some aspect of its relative or absolute position in the page structure)

There's an example here of clicking something by its HTML element id rather than by its <a> text:


Ah I see! Thanks for following up; turns out your method is exactly what I needed for another one of my macros (which involves a website in one of those “edge cases”). Cheers!

1 Like

What would you do in the case where the element you want to click is a button (and as such is a graphic without text) where the underlying XPath is different depending upon the record from which you’re clicking it?

I’d like to use KM to click a button to log a call to a contact in my Salesforce CRM system. Clicking the “Log a Call” button creates a new call record that is related to the contact record from which the button is clicked. The XPath for the button is nearly identical on each record, but with a slight variation:


How would I best handle that scenario? I’m currently doing it using a screenshot of the button as a found image on the page, but using Path would be much more robust, so I’d like to switch to that if at all possible.


1 Like

I realize that this is an ANCIENT thread, but I'm working on something similar, and rather than trying to explain it all again . . . .

I'm trying to use the click Front Browser Link (I am using Brave Browser), and I can't seem to get it to work. Here are a couple of representative pages:

I need to click on the "Edit" link in the top middle of the page. Notice that the Edit link moves horizontally depending on the length of the person's name. So it's not really possible to use a "position" because it's never quite right.

When I use the "Click Front Brower Link "Edit", nothing happens.

So for now, I have a "move to" and "just move" action that "sometimes" gets me there, so I can just click. But most times I have to actually use the mouse to open this link.

Any ideas?


Keyboard Maestro v. 9.2, Mac OS 11.7.8