python - 有没有办法在 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()
解决方案
显式等待和隐式等待是自动化的。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 是等待的毫秒数,你可以在这里使用任何定位器
推荐阅读
- firebase - 使用带有 Firebase 的 Mutex 来锁定文档,同时在扩大时避免多次重试尝试
- android - 无法读取包含 SpannableString 而不是常规字符串的通知
- vba - 使用 Cookie 身份验证和 CSRF 令牌连接到 Web API
- angularjs - 如何使用量角器定位自定义指令中的元素?
- pointers - Golang 中的 *uint 和 uintptr 有什么区别?
- sql - SQL选择列基于左连接后另一列的MIN值
- excel - 粗体 E 列中前 50% 的总和值及其行
- ios - 在 Firebase 实时数据库中存储数据
- javascript - 在 javascript 中传递两个 GET 变量
- kubernetes - 我可以在哪里动手学习 Kubernetes