Didn't understood, how if\else loop should work in Sikuli

44 views Asked by At

So, a couple of days ago i wanted to automate likes in some dating-app and choose SikulixIDE for this task. Wroted something like this:

while 1==1:
    Settings.ObserveScanRate = 1
    setAutoWaitTimeout(0)
    setFindFailedResponse(RETRY)
    region=Region(1710,270,210,637)
    if region.exists ("1704231969599.png"):
        region.click ("1704231969599.png")
    else:
        for num in range (10):
            type (Key.DOWN)

Search. If it finds - clicks. If it doesn't - scroll. Repeat. But after some random number of repeats it crashes in FindFailed Error. Why? I expected infinite loop of unsuccesful clicks, but not this.

I have other version of this loop. It based on ending of a script, that synchronised with an autoclicker. Clicker manually starts it all over again. Works absolutuely perfect. But it's kinda cringy ;)

P.S.: I am not a web-programmist or something, more machinist-guy and all programs, that i wrote was based on low-level languages (G-codes and other ISO-codes). So, maybe my question is really dumb and i just have an error in syntax. Also sorry for my English.

1

There are 1 answers

3
Raimund Hocke On

This is RaiMan from SikuliX.

The problem probably is created by the use of

setAutoWaitTimeout(0)
setFindFailedResponse(RETRY)

The first option switches off the internal search retries. The second one switches on the global search retry, which is done after all internal retries failed (none in your case) and always throws FindFailed if not found on retry.

So probably the exists() always finds the image with only one search, but there might be situations, where the app itself slows down a bit or other apps influence the GUI behavior, so that the image does not get visible within about 0.5 secs in your case. So you get the global FindFailed, which in turn ends the script.

Some general comments:

  • a loop should always have some criteria for ending
  • settings which are true for the lifetime of the loop should be done outside

So this is my proposal:

while True:
    region = Region(1710,270,210,637)
    if region.exists("1704231969599.png", 1): # max wait 1 sec for image
        region.click() # clicks the last match in that region
    else:
        for num in range(10):
            type(Key.DOWN)

Since I do not know the app behavior, I cannot make a suggestion for the ending of the loop.

Supposing you are running SikuliX 2.0.5