Find Image Matches - Skip an item in “For each” iteration based on condition


I need to click multiple instances of a button on the screen. The buttons are listed in random states (GREEN/RED) down the screen and in no particular order. I am using the Find Image Matches function to identify and then click all buttons in the “RED” state.

I am then running a “for each” function to click each “RED” button found on the screen.

Every now and then the software I am automating brings up an error screen when random “RED” buttons are clicked. This is how the software behaves under normal conditions so no issues with this.

If the error screen appears a “Cancel” button is clicked to acknowledge the error which returns me to the previous screen. What I want to be able to do is skip the “RED” button that brought up the error window and then continue to clicking the next “RED” button in the found matches.

The issue I have is that the when I return to the previous screen with the found matches the bot continues clicking the same “RED” button which keeps bring up the error message and won’t continue on to the next found image match, keeping me in an endless loop.

How would I go about removing the found match causing the error from the array so that when I return to the previous screen the bot will go on to clicking the next “RED” button in the array?

One possible solution - I have included a condition which checks if the error screen appears (using Image Exists) after the “RED” button is clicked within the For each loop. If the condition is met, is there a way to mark the item or delete the item from the found image matches to that it does not keep looping on the same found image?