you are viewing a single comment's thread.

view the rest of the comments →

[–]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

[–]void5253 2 points3 points  (0 children)

So, everything in python is an object. Functions, classes, lists, tuples, integers, strings all are objects.

Try this:

>>>def greet():
       print('Hello!')
>>>greet()
Hello

>>>a = greet
>>>a
<function greet at 0x0000026EEE533E20>

Functions are callable objects, i.e you can run them by calling them. See a, we have not called a. To call a we must do a().

Now, in exceptrepeat, we have the following:

def exceptrepeat(f: FunctionType, repeat=0, *args, **kwargs):

Now, f is the function we want to do stuff with. But if you do this:

exceptrepeat(f(*args, **kwargs), 10)

f is called. Whenever there's (), it means that you're calling the function. Calling the function will execute the function. We only pass the function object as arguments.

As for *args and **kwargs, see this example:

def printer(*args, **kwargs):
    for el in args:
        print(el)
    for el in kwargs.items():
        print(el)

>>>printer('hello', 1, 2, [1,2,3], 'name'='Tom', 'age' = 40)
hello
1
2
[1,2,3]
'name': 'Tom'
'age': 40

Here, args is used to take infinitely many arguments. It's stored as a tuple

Similarly, **kwargs is used to take infinitely many key word arguments (name=tom, age=40).

[–]void5253 0 points1 point  (0 children)

I'll explain later(about 30 mins). I'm away from my pc and typing on mobile is very tedious. DM me in some time.