Redirect to Beginning of Loop / Different Line

Asked by Dylan McCarthy

We're working on a project designed to go to a website, download a text file, import it into Excel, and save it, then loop and do the same thing again, for a different search criteria.

Question: Is it possible to have a 'if find ():' method, and when that specific 'if find' is completed, Sikuli will start over at the beginning of the code?

Example:
-----
setThrowException(False)
while not find (IMAGE):
     RANDOM STUFF HERE
     if find (IMAGE):
       click(IMAGE)
       **START OVER AT BEGINNING OF CODE**
exit()
-----

Any and all help is greatly appreciated.

Thanks,
-dwmcc

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Dylan McCarthy
Solved:
Last query:
Last reply:
Revision history for this message
Tsung-Hsiang Chang (vgod) said :
#1

You need an extra level of loop and a break statement.

while True:
   setThrowException(False)
   while not find (IMAGE):
      RANDOM STUFF HERE
      if find (IMAGE):
        click(IMAGE)
        break
   # you may need another break, otherwise this script never stops.

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

For situations like this I always recommend to use exists() instead of find() ( and wait()):
- in case of found it returns the match (no need for setThrowException())
- it does not raise exception FindFailed, just returns None in case of not found
- additionally you can set a waiting timeout, which can be 0, so it returns at once (no unnecessary waiting time)
- so you still have exception FindFailed to stop your script in cases, where you did not expect it

so in your case:

while not exists(IMAGE, 0): # no wait, because it should be there
     # RANDOM STUFF HERE
     if exists(IMAGE): # waits max 3 seconds
         click(IMAGE)
         # **START OVER AT BEGINNING OF CODE**
        continue # if it is not the last statement in loop
exit()

tip:
    if exists(IMAGE): # waits max 3 seconds
         click(IMAGE)
 if in this case, IMAGE is the same, there is no need for the additional search in click(), just say:
    if exists(IMAGE): # waits max 3 seconds
         click(getLastMatch())

More info in the docs.

Revision history for this message
Dylan McCarthy (dylan-dwmcc) said :
#3

RailMan - 'continue' was exactly what I was looking for!

Thanks for the help!
dwmcc