How to delete all images I found?


Can some one let me know how to count all the images I indicated and then click the found images one by one to delete them all? (The image is delete button, the image number is not fixed number. See attached jpg file for detail. )

I tried following code

Repeat until imagefound (“DeleteImage”)
click “DeleteImage”
End Repeat

Thanks in advance.


This should do what you want:

Repeat while imageFound("deleteImage")
    Click foundImageLocation()
End repeat


It works.


By the way, your original code would only execute if the image wasn’t found (and would have failed, since it wasn’t finding the image). “Repeat until” means “do this until you find the image, then don’t do it anymore.”

Matt, would this work too?

If you expect there to always be 1 or more copies of that image on the screen when you call the code, you could optimize the performance a little bit with the following:

Repeat for each location in EveryImageLocation(“DeleteImage”)
Click location
end repeat

When you perform a search that doesn’t find a result, it can take a fairly large amount of time (compared to a success). This loop should only give a failed search only if there isn’t a DeleteImage on the screen the first time it is called.

I considered that approach, but it tends not to work when you’re deleting things from the interface. The removal of the first item invalidates the locations of the rest of the elements. The time to fail to find an image once isn’t that significant in the overall execution time for a script, and if it is something that you really care about, you can add a time parameter to the imageFound command to constrain the search time.

Why would it invalidate the later locations? Does it search again?

Look at the image in the original post. Now think about the list of coordinates that will be returned by everyImageLocation(), each one corresponding to the initial position of one of the delete buttons. Now imagine that you click that first delete button and that it removes the corresponding item from the display. In many such systems, the item will disappear and all the elements below it will move up, so they will no longer be at the coordinates returned by everyImageLocation() before the delete button was clicked. You might be able to work around this by working from the end of the list back to the beginning, but that might also cause more delete buttons to scroll into view that aren’t currently displayed on the screen, and those won’t be represented in the list you’re iterating over.

The approach that I recommended will continue to click delete buttons for as long as they appear on the screen, so even if more scroll into view, they will get clicked. The only way that would happen with everyImageLocation() is if you put it in a loop, and you’d end up doing more searches than you would if you were just looking for the first instance each time.

EveryImageLocation() works really well when you need to identify a number of elements that aren’t going to be affected by your interacting with one of them, but if clicking one of them changes the locations of the others, then you lose the benefit of it and it actually becomes much less efficient.