0

I'm trying to load a URL using selenium get function, sometime it does not loaded and I have to reload it again. I'm using this code,

       while flag:
           try:
               driver.get(url)
               EC.presence_of_element_located((By.TAG_NAME, "body"))
               flag = False
           except:
               driver.get(url)

I can also get an exception inside except, How to handle this? One way is to add one more try-except inside except but I don't want to do this. I want to keep trying unless link opened.

1 Answer 1

1

First, start with the Error you are getting.

Second, you are using while flag: so in the except you don't need to do much maybe just log the errors or count the attempts

Then you can use the errors to handle them:

while flag:
    try:
        driver.get(url)
        EC.presence_of_element_located((By.TAG_NAME, "body"))
        flag = False
    except TimeoutException as t_e:
        print(t_e)
    except StaleElementReferenceException as s_e:
        print(s_e)
    except UnableToSetCookieException as u_e:
        print(u_e)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting timeout exception.
@AbdulHaseeb you are using while flag: so in the except you don't need to do much maybe just log the errors or count the attempts.
Yes answer in your comment section worked for me. Please update it in Answer section so that I can upvote it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.