if not found image skip to --- use exists() instead of find()

Asked by T

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 )
 wait(2)
 type(accountandpassworddetails)
  wait(2)
 else:
 type(accountandpassworddetails)
 wait(2)
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?

Question information

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

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
   wait(2)
   # some more code
# after the if

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

RaiMan suggests this article as an answer to your question:
FAQ #1501: “if not found image skip to --- use exists() instead of find()”.

Revision history for this message
eduardobedoya (gonnabdh) said :
#5

Hi RaiMan
I was looking at this question
cuz I needed to script something like... if image not found then...
so, Im jut wondering,
Is there a way to do something like this???

print A
print B
if exists(<logout-image>, 3)) #wait 3 seconds
    click(getLastMatch()) #if exists click on it
print C
print D

where: print ABCD mean the workflow
so if <logout-image> doesn't exist
then do not continue with print C, print D, but instead...
App.setClipboard("image not found")
    exit()

This can only be achieve with
try:
except
right??
or it can also be achieve with the "if exists" syntax?

I just was curious about this particular syntax
Thanks Advanced.

Can you help with this problem?

Provide an answer of your own, or ask T for more information if necessary.

To post a message you must log in.