python - How can I make Selenium click through a variable number of "next" buttons? -
i have internal web application has modal dialog. unfortunately cannot post actual web application location here, let me describe best possible.
- when application starts, box on screen tells bunch of text. can press "next" next page of text.
- on final page, "next" button disabled, , rest of ui of web application enabled.
- there variable number of pages, don't know how many times have click "next".
i'm able click through fixed number of times (ex: if know there's 2 pages can click twice) unsure how vary it'll run no matter number of pages have. general solution; presumably uses loop of sort check if button enabled. if is, click it. if it's disabled, exit loop.
the question is: how can set loop in selenium clicks button repeatedly until disabled?
here's code i've tried:
from selenium import webdriver selenium.common.exceptions import timeoutexception selenium.webdriver.support.ui import webdriverwait # available since 2.4.0 selenium.webdriver.common.by import selenium.webdriver.support import expected_conditions ec # available since 2.26.0 # create new instance of 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 introduction text... problematic code. # here tried wait element clickable, tried while # loop can click on long it's clickable, never seems # 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 i'd other stuff.... stuff want in test case." finally: driver.quit()
figured out. here's 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 found out:
- you can check if element enabled using
is_enabled()
. - you have re-search dom element after clicking on it. i'm guessing dialog redraws itself, need find again.
i can refactor nicer basic idea here now.
Comments
Post a Comment