Trap Error

Asked by Jeff_Vallis

Looked at https://answers.launchpad.net/sikuli/+question/101431
example given works fine but cannot work out how to implement it
My Script
Many:-
def something_1():
      find click or wait
def something_2():
      find click or wait
def something_3():
      find click or wait
def something_4():
      find click or wait
def something_5():
      find click or wait
# My Main
def This_Is_Main()
     something_1()
     something_2()
     something_3()
     something_4()
     something_5()
# Run Main
This_Is_Main()

what I want to do is trap any errors - add 1 to a counter and do
This_Is_Main() again if the counter is less than 3
I know that the screen that I am looking at changes occasionally and any find errors can be ignored
also because the next task in the list needs to run then I don't want an exception to stop the whole process
this part of the task is done with
/Applications/Sikuli/runsikulix -r /Users/Jeff/Desktop/SikuliScripts/Run_Startup.sikuli >> /Users/Jeff/Desktop/SikuliScripts/This_log.txt
where I keep a record of errors in This_log.txt

Can anyone please help

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

if I understand right:

--- use exists() for searching images:

def something_1():
    if exists(someImage):
        # do something
        return 0
    else:
       return 1

... and more of these

def This_Is_Main()
     failures = 0
     failures += something_1()
     failures += something_2()
     failures += something_3()
     failures += something_4()
     failures += something_5()
     return failures

shouldRepeat = True
counter = 0
while shouldRepeat:
     shouldRepeat = 0 < This_Is_Main()
     counter += 1
     if counter == 3:
        break

... or if I did not understand right: these are the building blocks to do whatever you want

Revision history for this message
Jeff_Vallis (vallis-pj) said :
#2

Not what I am trying to avoid
the problem is :-
if exists(an image)
    doubleClick(the same image)
Now you would think this is safe however the delay between checking if it exists and doing the double click very occasionally means that the image is no longer there - my log file proves this.
So I examined your code on the link above using try: and exception.
( an aside comment - if m: # this is a FindFailed "never happens" - m = reFindFailed.match(str(errB)) "must have changed" )
but ignoring that I see that I can trap the error by putting This_isMain() after a try and indenting it - but cannot figure out how to do this more than once on the example above e.g.
except:
   your code without the popup
   This_Is_Main() - result - on the first error it starts again but now I am outside of the try:
but here I am asking for help with my solution to exists/doubleClick problem and may be approack=hing it in the wrong way.

explaining very occasionally :-
The Procedure has 80 Exists followed by click or doubleClick
in one session the Procedure is called 20 times - 1600 exists
the session is done 4 times in one day - 6400 exists
very occasionally - maybe 2 or 3 times in 6400
but if I could just figure this out then no fails

B.T.W. you were right
running from the command line solved my previous problems were I was running direct from the editor - so instead of input I now use a file to set the parameters for each session

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

ok, that was not obvious in you question.

I understand:
you have a situation (occasionally), where you find an image using exists(image) and after a short time later, this image might have vanished already (occasionally), so a following click(image) causes a FindFailed error and stops the script at this point.
If this ever happens, you want the script to continue.

To generally avoid delays between the find op and a click, you should use
Settings.MoveMouseDelay = 0
to not have this "animated" mouse move, which lasts 0.5 seconds in the standard setting.

But with this, you still have some milliseconds delay.

# solution 1:
try:
    click(image)
except:
    pass # do nothing (or whatever you want)

# solution 2:
if exists(image):
    click(getLastMatch()) # does not search again, uses the match from the exists
else:
    pass # do nothing (or whatever you want)

Both solutions are equivalent in the outcome:
if the image is found, it is clicked with some milliseconds delay.
If the image is not there, no click happens and continues with the stuff in the except/else section

This can still happen:
If the image vanishes during the some milliseconds after the find op has finished and returned the match region, but before the click is done at the center of the match region, then the click might trigger an unwanted action depending on the state of the GUI in that moment at this point.
… but this cannot be avoided generally: what ever you do, to check if the image is still there, it will be some milliseconds delay, before the mouse click is issued.

Can you help with this problem?

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

To post a message you must log in.