why does a FindFailed exception stop my script? --- use exists() instead of find()

Created by RaiMan on on 2011-03-04
findfailed if exists find
Last updated by:
RaiMan on on 2011-06-20

I'm using sikuli to book into gym classes online for my work colleagues. I have the basic functionality working, but one of the flaws about the log in process is that if someone logs in to check their details and doesnt log out, the script to log in bugs out and the whole process fails. Therefore I need it to check on the first page it goes to, if LOG OUT is an option.

I found a similar question on this and tried the solution but i get a Cannot find img error before it even gets to the if.

img = ( LOGOUT)
if find(LOGOUT ):
        click( logout )
bugs out on first line. Causing me headache.

any ideas? and what would I have to do to make it look for one of 2 pictures, like logout or page is currently unavailable?

--- Answer

find() cannot be used in if/elif/while, since it does not return if not found, but stops the script with a FindFailed exception.

The appropriate method is exists(), which is functionally equivalent, but returns None in this case.

the most compact form for such cases:

click(exists(<logout-image>, 0)) # , 0) means only search once, no 3 seconds wait for image

this would click the match found by exists() or does nothing in case not found.

or in long form if you want to add more scripting:

if exists(<logout-image>, 0):
   click(getLastMatch()) # avoids another find() operation
   # some more code
# after the if