我想重复执行一个动作一定次数的 for 循环

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

我的设置如下所示:

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

然后我用它获取页面上的所有元素,并对每个元素执行一次操作:

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) 

我的问题是我想在循环 运行 大约 10 次后停止。我怎样才能做到这一点? (脚本本身工作正常顺便说一句)

您可以在代码中使用 enumerate

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