How can I make Selenium a click on a variable number of โ€œnextโ€ buttons?

I have an internal web application with a modal dialog. Unfortunately, I cannot post the actual location of the web application here, but let me describe it as best as possible.

  • When the application starts, you get a window on the screen that tells you a bunch of text. You can click "Next" to get the next page of text.
  • On the last page, the Next button is disabled, and the rest of the web application user interface is turned on.
  • There is a variable number of pages, so I donโ€™t know how many times I have to click Next.

I can click a fixed number of times (for example: if I know that there are two pages that I can double-click), but I'm not sure how to change this so that it works no matter how many pages I have. I would like a general solution; presumably this uses some kind of loop that would check if the button is on. If so, click on it. If it is disabled, exit the loop.

Question: How to set up a loop in Selenium that presses a button several times until it shuts down?

Here is the code I tried:

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 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 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() 
+7
source share
2 answers

Figured it out. Here is the corresponding code block:

 wait.until(EC.element_to_be_clickable((By.ID, 'main_buttonMissionTextNext'))) while EC.element_to_be_clickable((By.ID,'main_buttonMissionTextNext')): driver.find_element_by_id("main_buttonMissionTextNext").click() if not driver.find_element_by_id("main_buttonMissionTextNext").click().is_enabled(): break wait.until(EC.element_to_be_clickable((By.ID, 'main_buttonMissionTextNext'))) 

Two things I learned:

  • You can check if an element is enabled with is_enabled() .
  • You need to re-search the DOM for the element after clicking on it. I assume the dialog is redrawn, so you need to find it again.

I will most likely reorganize this to look better, but the main idea is here now.

+1
source
 While driver.find_element_by_id("main_buttonMissionTextNext").isEnabled() { driver.find_element_by_id("main_buttonMissionTextNext").click(); sleep(1); } //sleep function is in java, so you can "translate" it in python void sleep(int i){ driver.manage().timeouts().implicitlyWait(i, TimeUnit.SECONDS); } 

try and tell me that.

0
source

All Articles