Steve H Steve H - 2 months ago 7
Python Question

Using findall and clickall without getting an error if image doesn't exit

I can't figure out what I'm doing wrong. The code works great as long as there is an image displayed for the

findall
, but if
x
doesn't appear, then I get an error:

[error] FindFailed ( can not find P(1474201252795.png) S: 0.99 in R[0,0 1920x1080]@S(0) )


Not quite sure how to fix this.

running = True

def runHotkey(event):
global running
running = False

Env.addHotkey(Key.F1, KeyModifier.CTRL, runHotkey)

while exists("1474199877323.png")and running:

click("1474138615993.png")
click("1474138629993.png")
wait(5)
if exists("1474201633804.png"):
for x in findAll(Pattern("1474201252795.png").exact()):
click(x)
click("1474201809505.png")
else:
click("1474201689791.png")
wait(5)

Answer

As per documentation findAll throws an Exception on failed search. (docs). Try to use hasNext() method along with context manager, e.g.

with findAll(Pattern(...)) as mm:
    while mm.hasNext():
        x = mm.next()
        // process x
Comments