【问题标题】:Continue for loop at error item after unexpected error handling意外错误处理后在错误项处继续循环
【发布时间】:2017-08-11 07:59:57
【问题描述】:

设置

我有一个 url 列表,每个 url 都包含一个表单。我使用 Selenium 来填写表格,然后遍历网址。即

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

在哪里urls = [] # list containing all urls。这很好用。


问题

我不时收到其中一个网址的意外错误。例如,错误是由于该特定 url 没有特定字段造成的。

我调整了代码,以便能够处理所有缺少该特定字段的网址。一切都很好。

但是,我需要从头开始重新启动循环——效率不高。

有没有办法告诉 Python 从导致错误的 url 而不是从列表中的第一个 url 重新启动循环?

【问题讨论】:

  • 你试过try except else吗?

标签: python loops exception


【解决方案1】:

不必告诉python从那个点开始,而是使用'try''except'。这将简单地跳过破坏循环的 url,并继续直到它遍历所有 url。您还可以包含一个打印语句来确定哪个 url 不起作用,然后再返回它

所以,

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

【讨论】:

    【解决方案2】:

    您可以在代码的异常处理部分使用continue

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

    【讨论】:

      【解决方案3】:

      如果你使用try except else,可能如下:

      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
      

      【讨论】:

        【解决方案4】:

        您可以使用whiletry/except

        假设你的函数返回True:

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

        然后你可以重试直到成功。

        核心思想是你不需要重新启动当前的for循环,但你可以将你的函数包装在一个内部的while循环中。

        【讨论】:

        • 是的,对不起。
        • 无需道歉。只是让你知道;-)
        【解决方案5】:

        我猜你正在调试你的代码,你需要从错误输出 url 运行你的代码。 正如每个人建议的那样,tryexcept 块可用于处理错误。但出于调试目的,以下是一些调整

        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
        

        您可以从错误输出 url 而不是从列表的开头调试代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-02
          • 2012-05-29
          • 2014-12-25
          • 1970-01-01
          相关资源
          最近更新 更多