using "while not exists" to wait for an image and then click on it.

Asked by someotherperson432423423

Pretty simple, I want to wait for some button to come up. Then I want to click it. I browsed around for the best way and settled on

while not exists(image,0)
    wait(1)
click(image)

So I do that and it just spins when the image appears and does not click it. Probably a stupid mistake but I just can't see where.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
someotherperson432423423
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

I guess it is not the missing colon:
while not exists(image,0):
probably a typing error here. should have given a syntax error.

since I don't know, what's really happening, I tell you what I prefer:
- never search for an image again, that was already found

so

while True:
   if exists(image,0): break
   wait(1)
click(getLastMatch())

or

while True:
   match = exists(image,0)
   if m: break
   wait(1)
click(match)

If the click() does not happen: does your target have the focus? use switchApp() if necessary:
while True:
   match = exists(image,0)
   if m: break
   wait(1)
switchApp(app)
click(match)

some people use doubleClick() in such cases - the first click aquires the focus, the second clicks. For me it is more a workaround and does not work in all cases, so I prefer switchApp().

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#2

Yea, missed the colon sorry. What's happening is that it'll get to the point where I want to wait for the image, but it appears and never breaks out of the loop to click it. I'll try wrapping it in an while-if and see if it works though =), thanks.

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#3

So the code below is what I'm using, pretty much just took your code and added the image I was looking for. Same exact behavior. Sits there and hangs. The mySCR thing is a workaround I found to deal with my multiple screens in another post.

 #Wait for button, then click it
   while True:
 if exists( Image ,0): break
 wait(1)
   mySCR.click(getLastMatch())

Revision history for this message
RaiMan (raimund-hocke) said :
#4

So you are windows multiscreen? Sorry, that I did not ask earlier.

if so, look at: bug 606405 and take the workaround if applicable.

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#5

I used a different work around. I just switched which monitor was my main display and did this:

mySCR = Screen(1) # to avoid a new screen object with every action
 mySCR.click( image )

Which seems to work just fine, unless the thing that is currently breaking is somehow different then other normal clicks.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

I don't really understand.

Do you have still open issues related to your initial question?

If not, then set this to solved and have fun with Sikuli.

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#7

Yes, it's still not working. That bug you linked doesn't apply to me because I have another work around for multi-screen, and all the clicks work on multiscreen just fine except for my problem, which is still not working.

Revision history for this message
RaiMan (raimund-hocke) said :
#8

ok, then back to your snippet:

if all this happens on Screen(1), you have to qualifiy all actions:
myScr=Screen(1)
#Wait for button, then click it
while True:
   if mySCR.exists( Image ,0): break
   wait(1)
click(mySCR.getLastMatch())

or:
myScr=Screen(1)
#Wait for button, then click it
with mySCR:
   while True:
      if exists( Image ,0): break
      wait(1)
   click(getLastMatch())

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#9

Here's the entirety of my code so maybe you can see my problem

mySCR = Screen(1) # Work around for multiscreens on windows

while(1):
 #Click some buttons
 mySCR.click( image ) #These work fine
 mySCR.click( image )
 mySCR.click( image )
 mySCR.click( image )

    #Wait for button to appear, then click it
 while True:
  if exists( image,0): break
  wait(1)
 mySCR.click(getLastMatch())

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#10

Oooh, crap, forgot about last match. Stupid stupid. Lemme see if that works.

Revision history for this message
someotherperson432423423 (someotherperson432423423-deactivatedaccount) said :
#11

Sorry for the hubbub, I'll have to get that book when you publish it so I can use this tool more awesomely. Totally works now.

Revision history for this message
RaiMan (raimund-hocke) said :
#12

always welcome - no sorry for nothing.

Have fun.