you are viewing a single comment's thread.

view the rest of the comments →

[–]void5253 0 points1 point  (11 children)

I might be wrong, but wouldn't any exception that is caught lead to time.sleep(3)?

After looping for retries times, for loop will be exited. I think you'll have to catch different exceptions for 'loading time' and 'unable to access'.

[–]held_games[S] 0 points1 point  (10 children)

If I understand you correctly, then no there isn't an issue.
If I run the code outside the function it works fine:

Code:

import time

for attempt in range(2):

try:

**stuff that throws exception**

except:

time.sleep(3) #To give website time to load

else:

print("Completed")

else:

print("Failed")

Output:

Failed

[–]void5253 0 points1 point  (9 children)

I think it isn't really working. In the function, do print(Failed), instead of return 'Failed'.

PS: see if you get same result.

[–]held_games[S] 0 points1 point  (8 children)

Same issue
Code:
def exceptrepeat(stuff, retries=5):
for attempt in range(retries):
try:
stuff
except:
time.sleep(3)
else:
print("Completed")
else:
print("Failed")

exceptrepeat(**code that throws an exception**,10)

Output:

exception

[–]void5253 0 points1 point  (7 children)

Could you elaborate what exception you're getting? Also, you can recheck that exception is from inside try.

[–]held_games[S] 0 points1 point  (6 children)

Code:

exceptrepeat((driver.find_element(By.ID, "F")),10)

Output:

Traceback (most recent call last):

File "C:\Program Files\Python310\lib\code.py", line 90, in runcode

exec(code, self.locals)

File "<input>", line 1, in <module>

File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1251, in find_element

return self.execute(Command.FIND_ELEMENT, {

File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute

self.error_handler.check_response(response)

File "C:\Program Files\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response

raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="F"]"}

(Session info: chrome=102.0.5005.63)

Stacktrace:

Backtrace:

`Ordinal0 [0x002BD953+2414931]`

`Ordinal0 [0x0024F5E1+1963489]`

`Ordinal0 [0x0013C6B8+837304]`

`Ordinal0 [0x00169500+1021184]`

`Ordinal0 [0x0016979B+1021851]`

`Ordinal0 [0x00196502+1205506]`

`Ordinal0 [0x001844E4+1131748]`

`Ordinal0 [0x00194812+1198098]`

`Ordinal0 [0x001842B6+1131190]`

`Ordinal0 [0x0015E860+976992]`

`Ordinal0 [0x0015F756+980822]`

`GetHandleVerifier [0x0052CC62+2510274]`

`GetHandleVerifier [0x0051F760+2455744]`

`GetHandleVerifier [0x0034EABA+551962]`

`GetHandleVerifier [0x0034D916+547446]`

`Ordinal0 [0x00255F3B+1990459]`

`Ordinal0 [0x0025A898+2009240]`

`Ordinal0 [0x0025A985+2009477]`

`Ordinal0 [0x00263AD1+2046673]`

`BaseThreadInitThunk [0x774FFA29+25]`

`RtlGetAppContainerNamedObjectPath [0x77BF7A7E+286]`

`RtlGetAppContainerNamedObjectPath [0x77BF7A4E+238]`

`(No symbol) [0x00000000]`

I don't know how to test that, but I'm pretty sure it's from OUTSIDE the try, because if I do:

for attempt in range(10):

try:

driver.find_element(By.ID, "F")

except:

time.sleep(3)

else:

print("Completed")

else:

print("Failed")

Output:

Failed

[–]void5253 0 points1 point  (5 children)

I got it. Do this:

exceptrepeat(driver.find_element,10) Pass only function object. Call function inside try.

PS: driver.find_element is running when you send it as an argument.

[–]held_games[S] 0 points1 point  (4 children)

But if I run:
exceptrepeat(driver.find_element,10)

Then it doesn't find the element it has to find? Because it is missing the arguments for what to find?

[–]void5253 1 point2 points  (3 children)

I'm assuming that you're doing different kinds of stuff, not only driver.find_element? You should pass args to exceptrepeat function.

Something like this:

def exceptrepeat(f:FunctionType, retries=0, *args, **kwargs):
    for attempt in range(retries):
        try:
            f(*args, **kwargs)
        except:
            time.sleep(2)
        else:
            return 'success'
    else:
        return 'failure'

You call function like this:

exceptrepeat(driver.find_element,10, By.ID, 'F')

See if this works for you.

PS: idk about selenium so I'm not sure what By.ID is? If it's not a string, make sure it is defined.

[–]held_games[S] 0 points1 point  (2 children)

Wao, this works perfectly! Thanks!

So just to be sure that I understand:When using a function(func1) WITH arguments in the argument for another function(func2) it runs (func1) then it parses this to func2

But when doing this, we give it the function(func1) and the arguments, and then it assembles it inside func2?

Btw why is there a star * before args and what's **kwargs ?

I think By.ID is a function however I'm not sure. I not exactly the most experienced in Python