repeating findBest question

Asked by Ben Dix


I try to optimize my script from:

if exists(img1,0) or exists(img2,0) or ....:
     do something

to a better readable version by using a list and findBest.

So I made a list of images and repeat a function to click on different existing images, looking almost the same.
But in my case my function always clicks the same spot, found within the first round, even if it's not existing anymore.

any advice how to tell the function to search again new?

item_list = findBest{img1,img2,img3)

def doIt():
 for i in range(1,4,+1):
  if item_List:
   print('found and clicked item')
   sleep(random.uniform(0.4, 0.6))

Lclick() is my own def to click somewhere inside the region:

def Lclick(region):
    CoOrdData = (region)
    CoOrdDatax = CoOrdData.x + int(CoOrdData.w * random.random())
    CoOrdDatay = CoOrdData.y + int(CoOrdData.h * random.random())
    click(Location(CoOrdDatax, CoOrdDatay))

I am also wondering, if I can define a wait time like in using if exists(img,WAITTIME): for my list images?

Greetings 😄

Question information

English Edit question
SikuliX Edit question
No assignee Edit question
Solved by:
Ben Dix
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :

findBest returns only one Match object like exists does (it is the one with the highest match score of all images found).
For each image it only looks once. This is done for all images in parallel.

images = {img1,img2,img3)
match = findBestList{images)
if match:
    print "found: %d" % match.getIndex()


With 2.0.5 there are also available: waitBest/waitBestList and waitAny/waitAnyList, that take as first parameter the max time to wait for matches (not yet in the docs :-(

Revision history for this message
Ben Dix (bendix80) said :

thx Rainman,

now I figured out what to do 😄
Your example helps, of course, but now I understood that the moment you call "findBest" must be inside the loop to be able to search again for all images:

Images = [img1, img2,img2]

for i in range (1,5,+1):
    matchImages = findBestList(Images)
    if matchImages:

It is working only while using a wait command (or sleep) before calling the "matchImages" again.
Otherwise there seems to be not enough time to do a new search 😄

fine for me, so I can define my images somewhere als and call only the function when needed 😄