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:
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