Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to force python try run the same iteration again if an exception occurs?

I’m bulking a list of whatsapp contacts and the case (in a i-th [regarding wap number] iteration into a for loop) whether some element is not loaded until 2 seconds has a special meaning for my program which is not worth tell what that is but when this occurs I cannot send my message for the number regarding the current iteration so I’d try continue but when I do that approach the such number is ignored (since a used continue) and I’d going over and over my list again then I’d ended up getting a lot of unsended messages. I need a way to restart for loop when a exception in webdriverwait occurs to the iteraction which exception was showed up.

Ideal code:

for i in numbers:
    try:
       WebDriverWait(self.driver, 2).until(EC.element_to_be_clickable((By.XPATH, self.send_message_box_element)))
       send_message()
    except:
        go_back_and_try_again_send_message_to_number_aux()

I tried to manipulate the iterator i but it doesn’t work in python, python just forces i keep going over numbers:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    for i in numbers:
      try:
        i = aux
        del(aux)##it's necessary
      except:
        pass
       try:
           WebDriverWait(self.driver, 2).until(EC.element_to_be_clickable((By.XPATH, self.send_message_box_element)))
           send_message()
       except:
            aux = i
            go_back_and_try_again_send_message_to_number_aux()

>Solution :

You are asking to repeat the body of the for loop util it works, which sounds like the job of a loop:

for i in numbers:
    keep_trying = True
    while keep_trying:
        keep_trying = False
        try:
            ....
        except:
            #go_back_and_try_again_send_message_to_number_aux()
            keep_trying = True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading