Click on image when two images are found

I want to click an image but there are two same images on the screen and click on image action fails.

How can I click on the first image without having the script fail?

Click on which image? That’s why the action fails.

If you really want to force the issue, you can use a Find Image on Screen action to find the image, allowing for non-unique images, and then click at the resulting location.

Thanks Peter.

Let’s say I have 5 same images in the web page. How can I click on the 2nd image?

How can I refer the 2nd image data from FoundImage collection variable after finding the image?

Basically, you use the For Each action to iterate through the found images, from top to bottom, and keep track of how many to skip, and when there is no more, you deal with the image.

Here is an example.

Keyboard Maestro Actions.kmactions (9.3 KB)

1 Like

Thanks peter. It would be nice to specify the exact variable from the collection like: Image[1] (for the 2nd image) instead of going through loop. Hope to see that in the next version of KM.

That is not how collections work. If you want, you could generate an array of images, and then do whatever you want with them.

How to generate an array of images?

Set a variable Array to the empty string, use the For Each action I showed, and each time through the loop, if the Array variable is not empty, add a separator (eg “;”), then add the Image variable.

At the end, you have an Array variable that contains image details, separated by “;”.

You can use the text token %Variable%Array[3];% to access the entries.

Thank you, Peter.