简体   繁体   中英

Python: Catch Exception inside Except Block

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM