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)
3
  • I'm getting timeout exception. Commented Aug 25, 2020 at 6:28
  • @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. Commented Aug 25, 2020 at 7:26
  • 1
    Yes answer in your comment section worked for me. Please update it in Answer section so that I can upvote it. Commented Aug 25, 2020 at 15:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.