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

Time:05-17

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:             
    that.click()             
    time.sleep(this_time) 
         
    Action = self.browser.find_element_by_xpath("path")             
    Action.click()             
    Action.send_keys(Keys.ESCAPE) 

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)

CodePudding user response:

You can make use of enumerate in your code.

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