Click item on Chrome or Safari page by XPath

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:
/[@id=“downloadDocumentLink0”]
/
[@id=“downloadDocumentLink1”]
/*[@id=“downloadDocumentLink2”]
etc.

Thanks!

aal

@ComplexPoint

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.

-Chris

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 XPath.zip (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.:

//*[@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 :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

Julian

2 Likes

Well done Julian!

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

Hey Nikita,

That’s not macro — it’s a plug-in.

Drag the whole zip file to the Keyboard Maestro Editor icon in the Dock.

-Chris

I cannot get it to work!
version: keyboardmaestro 8.0.3

Error:
Task failed with status 1 in macro "Plug
In" (while executing Click Google Chrome item matching XPath applied to mouse position ).

I don’t know what the plugin is doing, but you can use XPath with the built in Click Browser Link action in Keyboard Maestro 8.

2 Likes

thank you very much

Is this plugin still functional in KM 8.0.5? I’ve installed it and pasted an xPath. I’ve tried all the “applied” option, without success. Wondering if I’m wasting my time. I tried the “Click Browser Link” action but it did not work. The named button I’m trying to click does not appear in the “list” and entering the name does not work.

I am trying to reduce my dependance on clicking at “found images”.

Can you show us the page and button, and the XPath that you are trying to reference it with ?

In the meanwhile, to obtain a valid XPath reference to a clickable element ( e.g. an <a> or <button> element in the HTML) :

  1. In Chrome (better equipped with XPath capture tools than Safari, even if it is Safari that you will target)
    ctrl-click the element of interest, and select Inspect from the contextual menu
  2. In the HTML view which then appears, select the enclosing <a href="..."> or <button> etc element
  3. Ctrl-click again, this time choosing Copy > Copy XPath

The clipboard should now contain an Xpath string like
//*[@id="post_26"]/div/div[2]/div[2]/section/nav/div/button[2]

which you can paste into a Click Browser Link action (the old plugin is now superfluous and no longer maintained).

1 Like

You generally do not need a plugin to click a link via XPath now as XPath support is built in to current versions of Keyboard Maestro.

1 Like

Thanks, both of you, for your response. Below are screenshots of the button and xPath from the code. I'm sorry, I cannot share the actual website without getting approval.

I've tried both the plugin and built-in action without success in this case. Maybe you can tell why by the images shared.