FindFailed Error when image is not on screen

Asked by MJ

for n in range(3):
#while (exists("1469038914742.png"),1):
#while True:
        if not exists("1469039051065.png"):
            click("1469038914742.png")
        else:
            click("1469042062223.png")
            click("1469038914742.png")

It spits out this error if image is not there how do i have the code end if image fails?

[error] script [ sikuli.sikuli3 ] stopped with error in line 5
[error] FindFailed ( 1469038914742.png: (23x34) in S(0)[0,0 1920x1080] E:Y, T:3.0 )

Thanks so much in advance

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
MJ
Solved:
Last query:
Last reply:
Revision history for this message
Roman Podolyan (podolyan-roman) said :
#1

Learn Python try ... except .

https://docs.python.org/3/tutorial/errors.html#handling-exceptions

Here is how I handle it:

def try_click(our_image):
    try:
        click(our_image)
        return True;
    except FindFailed:
        return False

If this code finds image, it clicks it and returns True. If does not find image, it just returns False, no Click executed.

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

to handle this error (FindFailed) see:
http://sikulix-2014.readthedocs.io/en/latest/region.html#exception-findfailed

another option always is, to use exists:

        if not exists("1469039051065.png"):
            if exists"1469038914742.png"): click()
            else: pass #do something else

Revision history for this message
MJ (mjpmgr) said :
#3

Thanks guys