How To Try Until Google Chrome Link Click Finds the Link and then Stop

I am very new and understand little of this. I am trying to automate the download of some documents. Google Chrome Click Link is working perfect to click on this /html/body/form/table[1]/tbody[2]/tr/td/div/table/tbody/tr[3]/td[8]/span [1]

And it's fine when it's only 1 link that is presented. But when there are several links I want to download only the most recent one. For example


I only want to click on link #4 but if link number 4 is not available then download link number 3 and stop, and don't download the next ones. Sometimes there can be up to 6 links, I would like you to check if link 6 is available, if not, check 5, if not 4 and if 4 is available download it.

I tried If this Then Else but it only works with 2 links, and when I try to nest multiple If this... functions it fails.

Note: I'm noob to use java or something like that.

1 Like

One simple but repetitive way to do it would be to wrap each click link action in a try catch block that attempts to click the link. If it's successful then it goes on to a cancel this macro action preventing clicking on further links. If it fails, the catch block will allow it to continue to the next try catch block. My example is looking at the Suggested Topics list of links at the bottom of each forum post on this site.

Test - select links but only the last one found.kmmacros (23.5 KB)

If you want to be a bit more programmatic you can loop until you get a link that works so you have less code. We count down from 6 to 3 and use the variable in the xPath to select the correct link and then cancel once we get the first one that works.

Test - select links but only the last one found loop.kmmacros (23.3 KB)