jeudi 15 février 2018

Selenium WebDriver Click skips some check boxes

This is a followup question to this:

WebDriver element found, but click returns nothing

But the issue is so specific to my case that I cant find an answer. I am running the below code in which after clicking on a few drop down menu items I need to make selections on state, districts and so on. I loop through different selections under 'State' item. I would also like the Road Wise button checked. Here is my code:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import os

chromedriver = r"C:\Users\yuppal\chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver

browser = webdriver.Chrome(chromedriver)
browser.implicitly_wait(10)
browser.get("http://omms.nic.in")
browser.maximize_window()

progElem = browser.find_element_by_link_text("Progress Monitoring").click()
summElem = browser.find_element_by_link_text("Physical and Financial Project Summary").click()


stateElem = browser.find_element_by_xpath("//select[@name='StateCode']")
state_options = stateElem.find_elements_by_tag_name("option")

del state_options[0]

def select_option(xpath, text):
    elem = browser.find_element_by_xpath(xpath)
    Select(elem).select_by_visible_text(text)

for option in state_options:

        select_state = Select(stateElem).select_by_value(option.get_attribute("value"))
        # Select the district.
        select_option("//select[@name='DistrictCode']","All Districts")   
        # Select the block.
        select_option("//select[@name='BlockCode']","All Blocks")   
        # Select the year.
        select_option("//select[@name='Year']","All Years")
        # Select the batch.
        select_option("//select[@name='Batch']","All Batches")
        # Select the funding agency.
        select_option("//select[@name='FundingAgency']","Regular PMGSY")

        # Check the road wise box.
        time.sleep(10)
        checkElem = WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, "//input[@title='Road Wise']")))
        browser.execute_script("arguments[0].click();", checkElem)

        # Click on the view button.
        time.sleep(10)
        browser.find_element_by_xpath("//input[@type='button']").click()



        # Switch to a new frame.
        time.sleep(10)
        frame = browser.find_element_by_xpath("//div[@id='loadReport']/iframe")
        browser.switch_to.default_content()
        #browser.switch_to.frame(frame)
        WebDriverWait(browser, 120).until(EC.frame_to_be_available_and_switch_to_it(frame))
        #browser.switch_to.frame(browser.find_element_by_xpath("//*[@id='loadReport']/iframe"))

        # click on the save button
        time.sleep(10)
        WebDriverWait(browser, 120).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Export drop down menu']"))).click()

        # Within the save button, Click on the "Excel" option.
        time.sleep(10)
        WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div/a[@title='Excel']"))).click()


        # Switch back to the main content.
        time.sleep(20)
        browser.switch_to.default_content()

My issue is the 'Road Wise' checkbox gets clicked for some iterations and not for others. I checked the html code and it is the same for all check boxes. I thought the 'view' button gets clicked before the road wise button is clickable. I put some waiting period before both road wise and view buttons. But doesn't seem to help. I would appreciate any help!

Yogesh




Aucun commentaire:

Enregistrer un commentaire