title: How can I make Selenium click through a variable number of "next" buttons?
body: <p>I have an internal web application that has a modal dialog. Unfortunately I cannot post the actual web application location here, but let me describe this as best as possible.</p>
  
  <ul>
  <li>When the application starts, you get a box on the screen that tells you a bunch of text. You can press "next" to get the next page of text.</li>
  <li>On the final page, the "next" button is disabled, and the rest of the UI of the web application is enabled.</li>
  <li>There are a variable number of pages, so I don't know how many times I have to click "next".</li>
  </ul>
  
  <p>I'm able to click through a fixed number of times (ex: if I know there's two pages I can click twice) but I am unsure how to vary this so that it'll run no matter what number of pages I have. I would like a general solution; presumably this uses a loop of some sort that would check if the button is enabled. If it is, then click it. If it's disabled, then exit the loop.</p>
  
  <p>The question is: <strong>How can I set up a loop in Selenium that clicks a button repeatedly until it is disabled?</strong></p>
  
  <p>Here's the code I've tried:</p>
  
  <pre><code>from selenium import webdriver
  from selenium.common.exceptions import TimeoutException
  from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
  
  # Create a new instance of the Firefox driver
  driver = webdriver.Firefox()
  
  driver.get("http://localhost/myapp")
  
  try:
      wait = WebDriverWait(driver, 100)    
      wait.until(EC.element_to_be_clickable((By.ID,'menuIntroBox_buttonNext')))    
      driver.find_element_by_id("menuIntroBox_buttonNext").click()
  
      # Click through the introduction text... this is the problematic code.
      # Here I tried to wait for the element to be clickable, then tried to do a while 
      # loop so I can click on it as long as it's clickable, but it never seems to do the
      # break.
      wait.until(EC.element_to_be_clickable((By.ID,'main_buttonMissionTextNext')))
      while EC.element_to_be_clickable((By.ID,'main_buttonMissionTextNext')):
          element = driver.find_element_by_id("main_buttonMissionTextNext")
          element.click()
          print "Waiting until it's clickable."
  
          if not element.is_enabled():
              break
  
      print "Here is where I'd do other stuff.... the stuff I want to actually do in the test case."
  finally:
      driver.quit()
  </code></pre>