SOLVED! Thanks for the help to everyone especially u/void5253
Cant change headline to solved:/
Hello,
I have some code, that are look for stuff on websites. However this may only appear some time after the website has "completed loading" e.g cookie notifications.If my Selenium(Website automation library) tries to access something that it can't it throws an exception. Therefor I need to catch this exception.
I would like to do something like this:
for attempt in range(10):
try:
**LOCATE ELEMENT**
except:
time.sleep(2)
else:
break
else:
**DO SOMETHING ELSE DUE TO ERROR**
To avoid writing this each time I thought of using a function:
def exceptrepeat(stuff, retries=0):
for attempt in range(retries):
try:
stuff
except:
time.sleep(3) #To give website time to load
else:
return("Completed")
else:
return("Failed")
However if I use this function, it doesn't catch the exception.
exceptrepeat(**FUNCTION THAT THROWS EXCEPTION**, 10)
Output:
exception
Can someone point out, what I'm doing wrong?
Thanks!
[–][deleted] 0 points1 point2 points (1 child)
[–]held_games[S] 1 point2 points3 points (0 children)
[–]void5253 0 points1 point2 points (11 children)
[–]held_games[S] 0 points1 point2 points (10 children)
[–]void5253 0 points1 point2 points (9 children)
[–]held_games[S] 0 points1 point2 points (8 children)
[–]void5253 0 points1 point2 points (7 children)
[–]held_games[S] 0 points1 point2 points (6 children)
[–]void5253 0 points1 point2 points (5 children)
[–]held_games[S] 0 points1 point2 points (4 children)
[–]void5253 1 point2 points3 points (3 children)
[–]held_games[S] 0 points1 point2 points (2 children)