How can Sikuli be used to wait for a button for a long time, with perhaps some maintenance task in between?

sakatc picture sakatc · Jul 25, 2012 · Viewed 41.1k times · Source

I have a webpage where I am waiting for a button to appear, and when it appears I would like to click it. The button is on a timer and may take as long as an hour to appear. Also, if the button takes longer than a certain length of time to appear, I'd like to move the mouse (otherwise the website will log me out automatically).

So, to wait for a button to appear I devised this Sikuli script:

button = "button.png"

while(1):
    if exists(button):
        print("found it")
        click(button)
        break
    else:
        print("wait longer")
        wait(button,30*60)
        # do a regular task

print "all done!"

The above does not seem to be functional. If the button is on screen, the script will find it... However, if it has to wait it will simply time out quickly with a FindFailed exception (on the click() even though the button does not exist on screen). I considered writing a handler, but seems like overkill.

What am I doing wrong and what is the best way to wait a long period for a visual event like this?

Answer

spearson picture spearson · Jul 26, 2012

Some other thoughts for you...

 while(1):
 wait(Button, 30*60) # This will spinlock for 30 minutes for the button to appear
 if exists(Button):
     hover(Button) # Debug statement allowing user to see what Sikuli has matched to
     click (Button)
 else:
     mouseMove(Location(50,100))
     mouseMove(Location(50,200))

Links: