Click in Chrome using XPath, but with flexibility for XPath variability?

I'm trying to set up a macro for Chrome and specifically for Gmail that uses Xpath to click on a button in certain e-mails I receive. The one issue I'm noticing is that the XPath varies in one specific way between e-mails. For example, here are XPaths from two such e-mails...

//*[@id="m_-7892333262802076651email_content"]/table/tbody/tr[4]/td[2]/table/tbody/tr[2]/td/a/table/tbody/tr/td/a/center/font/span

//*[@id="m_-1703101020033013197email_content"]/table/tbody/tr[4]/td[2]/table/tbody/tr[2]/td/a/table/tbody/tr/td/a/center/font/span

As you can see, the XPaths are identical except for the number at the beginning, which I'm guessing is some kind of e-mail ID.

With this in mind, is there any way to ignore this number and instead match by everything else in the XPath? If so, how would the macro and code here need to be updated...?

Thanks!

Have you tried clicking via CSSselector instead? I use SelectorsHub to get access to this (as well as XPath).

1 Like

Interesting. I'm not familiar with CSSselector. Is it basically the same concept and approach as the macro shared above? Is there a comparable CSSselector-based macro that I could use?

  • Grab the cssSselector using SelectorsHub:

CleanShot 2023-11-29 at 08.10.49

  • Insert the result into this Javascript:

document.querySelector('<CSSSELECTOR GOES HERE>').click();

In the below example, the copied cssSelector is #site-logo, so the full Javascript is:

document.querySelector('#site-logo').click();

  • Add this to an Execute Javascript in Front Browser action.

Click Via CSSselector (Example).kmmacros (38 KB)

Macro screenshot

1 Like

Thanks, @noisneil! So I installed the CSSselector extension and gave this a shot. The Rel cccSelector value I'm getting for the element in question is...

span[shub-ins='1']

I tried entering this value into the placeholder you indicated, but it doesn't seem to work. The macro triggers and does nothing. Any idea what I might be doing wrong?

Hi, @noisneil. Just curious if you have any idea why this might not be working. Thanks!

Without having one of the buttons to test with, I can't, I'm afraid. :man_shrugging:t2:

Can you show me what you typed into the action?

Here's a screenshot of the macro. I've gotten it to work in some other places, e.g. with other buttons in e-mails. But I've also encountered other situations where it's not working. I'm guessing the issue is with the cssSelector being pulled in, which I might be doing wrong.

Edit: In case it's helpful, the button is a call-to-action button in an e-mail notification from Facebook.

Try using the Relative XPath available in SelectorsHub in a Click Link in Front Browser action, instead of the explicit XPath.

If you have no luck, is it possible to share some details about the button you're trying to click?

Nice! That seems to have solved it.

With this macro, do you know how to update the script so that the "OK" or error message doesn't show when it runs? I see where it happens in the script, I'm just not sure what exactly to remove. Thanks!

I'm confused. Are you using the Javascript macro or the KM action I suggested?

Oh, I went back to using the original macro above, because I thought you said to use the XPath and not the CSSselector. Were you suggesting something else?

You can use the Relative XPath directly in this action.

I got the name slightly wrong; it's "Click Front Browser Link".

Ah, got it. Works! Is there a way to adjust this so that instead of opening the link in a new tab and taking you to that tab, the macro instead opens the link in a new tab but keeps you on your current tab?

Try this:

Click Link and Return to Starting Tab.kmmacros (21 KB)

Macro screenshot

1 Like

Perfect. Thanks, @noisneil. One other question. Is there a way to instead right click the link or element indicated by the XPath? How about middle click?

Not that I'm aware of. What would be the end goal?

It would be cool, using XPath, to still have the flexibility that "click at found image" gives you, where you're able to do any of left/center/right single/double click, and more. (As opposed to only being limited to left single click.)

I'm not aware of any way to do this, but fortunately I don't think I've ever needed to do anything but left-click a link.

1 Like