简体   繁体   中英

Continue for loop at error item after unexpected error handling

Set-up

I have a list of urls which each contain a form. I use Selenium to fill the form and I loop over the urls. Ie

for url in urls:
     browser = webdriver.Chrome()
     browser.implicitly_wait(30)
     browser.get(url)

     data = {} # dictionary containing variables to be inserted in the url's form

     var1 = browser.find_element_by_id("id")
     var1.clear()
     var1.send_keys(data['var1'])

     # here follow more variables to be inserted

where urls = [] # list containing all urls . This works fine.


Problem

Every now and then I receive an unexpected error for one of the urls. For example, the error results from that particular url not having a specific field.

I adjust the code to be able to handle all urls missing that specific field. Everything is fine.

But, I need to restart the loop from the beginning – not efficient.

Is there a way to tell Python to restart the loop from the url which resulted in an error, instead of from the first url in the list?

Instead of having to tell python to start from that point, rather use 'try' 'except'. This will simply skip the url that is breaking your loop, and continue until it has looped over all the urls. You could also include a print statement to identify which url didn't work, and then go back to it afterwards

So,

try:
     for url in urls:
         browser = webdriver.Chrome()
         browser.implicitly_wait(30)
         browser.get(url)

         data = {} # dictionary containing variables to be inserted in the url's form

         var1 = browser.find_element_by_id("id")
         var1.clear()
         var1.send_keys(data['var1'])
except Exception as e:
     print(url)
     print('Exception:',e)  
     pass

You can use a while and try/except :

suppose your function return True :

for url in urls:
    success = False
    while not success:
        try:
            data, success = your_function()
        except:
            success = False

Then you can just retry until it succeed.

The core idea is that you do not need to restart current for loop, but you can wrap your function in an internal while loop.

You can use continue in the exception handle part of the code.

for url in urls:
    try:
        code may have exception
    except:
        continue

If you use try except else , it could be as following:

for url in urls:
    browser = webdriver.Chrome()
    browser.implicitly_wait(30)
    browser.get(url)

    data = {} # dictionary containing variables to be inserted in the url's form

    try:
        var1 = browser.find_element_by_id("id")
        var1.clear()
        var1.send_keys(data['var1'])
    except Exception, exception:
        print exception # will print the error but ignore the lines of the else statement
        # or do something about the error
    else:
        print 'went well'
        # here follow more variables to be inserted
        # or continue your code

I guess you are debugging your code and you need to run your code from error out url. as every one suggested try except block can be used to handle errors. But for your debug purpose, below are tweaks

i = 0 # for first time. next time you can assign it to index of error generating url
while i < len(urls):
    try:
        url = urls(i)
        browser = webdriver.Chrome()
        browser.implicitly_wait(30)
        browser.get(url)

        data = {} # dictionary containing variables to be inserted in      the url's form

        var1 = browser.find_element_by_id("id")
        var1.clear()
        var1.send_keys(data['var1'])
    except:
        print i
        raise

You can debug you code from error out url and not from beginning of list.

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