I want to repeat a for loop that does an action for a set amount of times

My setup looks like this:

def WaitForObjects(self, type, string,):
    return WebDriverWait(self.browser,3).until(EC.presence_of_all_elements_located((type,string)))

Then i get all elements on a page with it and perform an action once per element:

that = self.WaitForObjects(By.CSS_SELECTOR,"class")
for this in that:             
    Action = self.browser.find_element_by_xpath("path")             

my problem is that i want to stop after like 10 times the loop has run. How can i do that? (script itself works fine btw)

>Solution :

You can make use of enumerate in your code.

for count, this in enumerate(that):
    if count == 10:
    # Add your remaining code here
    # .....
    # .....

Leave a Reply