前言:目前在使用selenium过程中,对于显示等待每次使用要写很长的内容,而对于expected_condition中的条件也不是很清楚,目前自己在使用过程中,为了方便实例化调用,特定二次封装了一下WebDriverWait模块
# coding = utf-8
# Author = zhaoge
# Date: 2021/6/24 5:43 下午
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class ExplicitWaiting(object):
"""
title_is -判断title是否出现,返回bool类型
title_contains -判断title是否包含某些字符,布尔
presence_of_element_located -判断某个元素是否被加到dom树里,并不代表该元素一定可见,WebElement
visibility_of_element_located -判断某个元素是否被添加到了dom里并且可见,宽和高都大于0,WebElement
visibility_of -判断元素是否可见,如果可见就返回这个元素,WebElement
presence_of_all_elements_located -判断是否至少有1个元素存在于dom树中,列表
visibility_of_any_elements_located -判断是否至少有一个元素在页面中可见,列表
text_to_be_present_in_element -判断指定的元素中是否包含了预期的字符串,布尔
text_to_be_present_in_element_value -判断指定元素的属性值中是否包含了预期的字符串,布尔
frame_to_be_available_and_switch_to_it -判断该frame是否是否可以switch进去,布尔
invisibility_of_element_located -判断某个元素是否存在于dom或不可见,布尔
element_to_be_clickable -判断某个元素中是否可见并且是enable的,代表可点击,布尔
staleness_of -等待某个元素从dom树中移除,布尔
element_to_be_selected -判断某个元素是否被选中了,一般用在下拉列表,布尔
element_selection_state_to_be -判断某个元素的选中状态是否符合预期,布尔
element_located_selection_state_to_be -判断某个元素的选中状态是否符合预期,布尔
alert_is_present -判断页面上是否存在alert,布尔
"""
def __init__(self,driver,timeout=20):
"""
:param driver: WebDriver实例
:param timeout: 超时时间,默认20s
"""
self.locationTypeDict = {
"id": By.ID,
"name": By.NAME,
"xpath": By.XPATH,
"css_selector": By.CSS_SELECTOR,
"class_name": By.CLASS_NAME,
"tag_name": By.TAG_NAME,
"link_text": By.LINK_TEXT,
"partial_link_text": By.PARTIAL_LINK_TEXT
}
self._driver = driver
self._wait = WebDriverW