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

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

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

>Solution :

You can make use of enumerate in your code.

for count, this in enumerate(that):
    if count == 10:
        break
    # Add your remaining code here
    # .....
    # .....
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