首页 > 解决方案 > 有没有办法在 Python Selenium 中对动态选择对象执行显式等待

问题描述

随附的代码有效,但效率不高。我想知道是否有人可以建议我如何在 Python Selenium 中对“动态选择对象”执行显式等待

我真的希望避免任何时间。睡觉。我已经在“fillfields()”部分的“variant-select”上进行了尝试。我将非常感谢任何建议

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()

标签: pythonseleniumwebdriverwait

解决方案


显式等待和隐式等待是自动化的。Selenium 有 Webdriverwait 类。

如果您编写等待方法并保留在项目中,则可以在需要等待操作时随时随地使用。

这是等待的方法

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

如果您等待 id 元素,您可以使用以下行

waitForElement(By.id(""),20); // 这里 20 是等待的毫秒数,你可以在这里使用任何定位器


推荐阅读