Robert Chestnutt
Robert Chestnutt

Reputation: 322

Is there a way to perform an explicit wait on a dynamic select object in Python Selenium

The code enclosed works but is not efficient. I was wondering if anyone would be able to advise me on how to perform an explicit wait on a 'dynamic select object' in Python Selenium

I was really hoping to avoid any time.sleeps. I have tried it on 'variant-select' in the 'fillfields()' section. I would be really grateful for any advice

def getfields():
    global manu_select, pre_current, model_select, variant_select, cont_type_s, paymentplan_s, term, mpa, VED_select, co2, calc_button, mth_rental, disc_total, dealer_supp, manu_supp, wait
    wait = WebDriverWait(browser, 20)
    manu_select = Select(browser.find_element_by_id('selManufacturers'))
    pre_current = browser.find_element_by_xpath('//*[@title="Check this box to include pre current models"]')
    model_select = Select(browser.find_element_by_id("selModels"))
    variant_select = Select(browser.find_element_by_id("selVariants"))
    cont_type_s = Select(browser.find_element_by_id("selContracts"))
    paymentplan_s = Select(browser.find_element_by_id('selPaymentPlan'))
    term = browser.find_element_by_xpath('//*[@id="txtTerm"]')
    mpa = browser.find_element_by_xpath('//*[@id="txtMPA"]')
    VED_select = Select(browser.find_element_by_xpath('//*[@id="selVEDRate"]'))
    calc_button = browser.find_element_by_xpath('//*[@id="btnCalc"]')

def fillfields():
    for i in range(len(df)):
        getfields()
        manu_select.select_by_visible_text(df['manufacturer'][i])
        pre_current.click()
        model_select.select_by_visible_text(df['model_name'][i])
        time.sleep(2)
        #variant_select = wait.until(ec.element_to_be_clickable((By.ID, 'selVariants')))
        variant_select.select_by_visible_text(df['derivative'][i])
        time.sleep(1)
        cont_type_s.select_by_visible_text(df['contract_type2'][i])
        time.sleep(1)
        paymentplan_s.select_by_visible_text(df['payment_plan'][i])
        term.send_keys(df['term'][i].astype(str))
        mpa.clear()
        mpa.send_keys(df['mileage'][i].astype(str))
        time.sleep(2)
        VED_select.select_by_value("01/04/2020 00:00:00")
        time.sleep(2)
        #co2 = browser.find_element_by_xpath('//*[@id="txtWltpCo2]')
        #co2.send_keys(df['co2'][i].astype(str))
        calc_button.click()
        mth_rental = wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="rentalPanel"]/div[1]/span[1]'))).text
        df.loc[i, 'mth_rental'] = mth_rental
        disc_total = wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="fsOTRBreakdown"]/div[1]/div[4]/span'))).text
        df.loc[i, 'disc_total'] = disc_total
        dealer_supp = wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="fsOTRBreakdown"]/div[1]/div[5]/span'))).text
        df.loc[i, 'dealer_supp'] = dealer_supp
        manu_supp = wait.until(ec.presence_of_element_located((By.XPATH, '//*[@id="fsOTRBreakdown"]/div[2]/div[6]/span'))).text
        df.loc[i, 'manu_supp'] = manu_supp
        lexQuotes()

Upvotes: 0

Views: 137

Answers (1)

Justin Lambert
Justin Lambert

Reputation: 978

Explicit wait and implicit wait are out of automation .Selenium has Webdriverwait class.

If you write wait method and keep inside your project you can use anytime and anywhere if you need wait actions.

This is method of wait

 private static WebElement waitForElement(By locator, int timeout)
{
    WebElement element=new WebDriverWait(driver,timeout).until(ExpectedConditions.presenceOfElementLocated(locator));
    return element;
}

in case if you wait for id element you can use the following line

waitForElement(By.id(""),20); // here 20 is miliseconsds to wait and you can use any locators here

Upvotes: 1

Related Questions