python-selenium -- 3种等待方式

一、等待 -- 3种等待方式

1.1 强制等待

#引入模块

import time

#某操作后 等待5s

time.sleep(5)

1.2 隐性等待

#隐式等待60s -- 全局可用

driver.implicitly_wait(60)

1.3 显性等待

明确等待某个条件满足之后,再去执行下一步操作。

WebDriverWait(driver,等待时长,轮循周期默认0.5s).until/until_not(expected_conditions模块.方法)

expected_conditions期望的条件模块中提供了一系列的期望发生的条件,每个条件方法均是一个类

如:WebDriverWait(driver,20).until(EC.presence_of_element_located(locator))

#引入显性等待库

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

#先确定元素定位表达式

ele_locator = '//a[text()="_百度百科"'

#元素定位方式--元组表达

param = (By.XPATH,ele_locator)

#WebDriverWait(driver,等待时长,轮循周期).until(判断条件)

#使用expected_conditions对应的方法生成判断条件 EC.方法名(定位方式,定位表达式)

WebDriverWait(driver,20).until(EC.presence_of_element_located(param))

expected_conditions模块中常用方法:

expected_conditions.presence_of_element_located() -- 元素存在

expected_conditions.visibility_of_element_located() -- 元素可见

expected_conditions.element_to_be_clickable() -- 元素可点击

expected_conditions.alert_is_present() -- 弹出框呈现

expected_conditions.element_located_to_be_selected() -- 元素被选中

expected_conditions.frame_to_be_available_and_switch_to_it() -- 切换到iframe进行操作

expected_conditions.new_window_is_opened() -- 打开一个新的窗口

猜你喜欢

转载自www.cnblogs.com/simran/p/9237449.html