Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Selenium click through a variable number of "next" buttons?

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.

  • 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.
  • On the final page, the "next" button is disabled, and the rest of the UI of the web application is enabled.
  • There are a variable number of pages, so I don't know how many times I have to click "next".

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.

The question is: How can I set up a loop in Selenium that clicks a button repeatedly until it is disabled?

Here's the code I've 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'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()
like image 552
Irwin Avatar asked Apr 05 '13 00:04

Irwin


People also ask

What are the different ways to click on a button in Selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.

How do I make something click in Selenium?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

What is click () method in Selenium?

For interacting with web elements in test automation, QA engineers use the Click command offered by Selenium WebDriver. It helps simulate the click action users perform in the real world. However, there are multiple subsets of the click action – left-click, right-click, double click, drag, and drop, etc.

How to handle different types of calendars in selenium?

How To Handle Different Types Of Calendars In Selenium: Part 1 1 We always see some input boxes where we need to select date from calendar widget. When we click on that input box, a... 2 You can see different types of calendar widget varying across applications. For example: More ...

How do I know when to stop taking selenium?

If you see a reduction in total antibodies in the serum then continuing supplementation may be a good idea. If you experience negative symptoms, no change or a worsening of your symptoms then it may be worth avoiding excess selenium from food sources and supplements (more information on which foods contain high selenium below).

How do I create next and Previous buttons on a slide?

Then, you can create your own Next and Previous buttons on the slide itself. These can be adjusted on the timeline to appear whenever you like. Create a True/False variable and name it something like NextSlide. Then, add a condition to your Next button trigger so that the variable NextSlide must equal True.

How long does it take for selenium to work?

In most cases, these symptoms start about 1 week after starting selenium and completely resolve within 4-5 weeks after cessation of the supplement. You can avoid all of these symptoms by using only high-quality selenium supplements which contain no more than 200-400mcg per serving.


1 Answers

Figured it out. Here's the relevant 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 found out:

  1. You can check if an element is enabled using is_enabled().
  2. You have to re-search the DOM for the element after clicking on it. I'm guessing that the dialog redraws itself, so you need to find it again.

I can likely refactor this to look nicer but the basic idea is here now.

like image 144
Irwin Avatar answered Nov 14 '22 23:11

Irwin