Trap for errors

Asked by kiki 3000

I am trying to make a sikuli script that will automate some jobs that i have on internet like adding my url on various web sites.
So i have list of urls that i must visit to add a site to add url.
For example:
http://www.surfsafely.com/urladd.html
http://www.cultuurtechnologie.net/submit.php
http://www.247webdirectory.com/submit.aspx
....
So, script must vistit each site and perform actions to add url.
That is not a problem, that is simple to do.
But what if script somewere encounters some problem on one web site and can not continue to next web site in list, like web site temporarely is offline, or the site is too slow becouse ISP have some technical problems?
The script just stopes. That is the problem. If i have list of cca 100 urls and script run on some problem with site number 46 in list then it just stopes. Can there be some solution to this. Like error trap that would go to next url in list not just stop if it encounter some problem and maybe write that url in some txt file or something.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

use exists() instead of find() or split a click() in exists()+click()

# using find() to test for ok
if not exists(something-that-should-be-there, timeout):
   print "URL did not work: ", current-url
   continue # go on with next in loop

# splitting a click
if not exists(something-that-should-be-clicked, timeout):
   print "URL did not work: ", current-url
   continue # go on with next in loop
click(getLastMatch()) # if found, all is ok and the stored match can be clicked

When you are sure your image should be there, set timeout to 0, so it only makes one try (max waiting time 0.5 seconds).

Revision history for this message
kiki 3000 (kiki3000) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Gromadusi (gromadusi+fwd) said :
#3

not being a friend of if's (which can sum up very quickly in a script), i prefer using try...except.

so, it would look like
try:
    find(Pattern)
except Exception, err:
     print "oops", err

if you know what failure it raises, you can even define the exception.
try:
    find(Pattern)
except FindFailed:
     print "URL did not work: ", current-url

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

@ Gromadusi

I don't think it is a question of being "a friend of something".

try: except: is a possibility, but in this case it would make it even more complicated, since the script has to be continued afterwards. So how do you know, that the exception was raised? You have to either pack all your code in try: or except (even nested, if you want to get the point) or use a variable, that tells you afterwards wether its ok or not.

isOK = False
try:
    find(Pattern)
    isOK = True
except FindFailed:
     print "URL did not work: ", current-url

if isOK:
   # executed when found
else:
   # executed when not found

That is not really readable. This is the reason, that a function like exists() is available.

I think

if exists(Pattern):
   # executed when found
else:
   # executed when not found

is short and readable.

I prefer this, so the FindFailed exceptions only come up, if something is really not expected.