diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 2a2592d..678e5d4 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -95,13 +95,19 @@ class DriverPage(object): return self.ele(loc, mode='all', timeout=timeout, show_errmsg=show_errmsg) # ----------------以下为独有函数----------------------- - def wait_ele(self, loc_or_ele, mode: str, timeout: float = None): - """ - :param loc_or_ele: - :param mode: show, hidden, del, - :param timeout: - :return: + def wait_ele(self, + loc_or_ele: Union[str, tuple, DriverElement, WebElement], + mode: str, + timeout: float = None) -> bool: + """等待元素从dom删除、显示、隐藏 + :param loc_or_ele: 元素、获取元素的字符串或元素的loc + :param mode: 等待方式,可选:'del', 'visible', 'invisible' + :param timeout: 超时时间 + :return: 等待是否成功 """ + if mode.lower() not in ['del', 'visible', 'invisible']: + raise ValueError('mode can only be "del", "visible", "invisible"') + from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as ec @@ -114,28 +120,35 @@ class DriverPage(object): is_ele = True elif isinstance(loc_or_ele, str): loc_or_ele = get_loc_from_str(loc_or_ele) + elif isinstance(loc_or_ele, tuple): + pass + else: + raise TypeError('The type of loc_or_ele can only be str, tuple, DriverElement, WebElement') if is_ele: # 当传入参数是元素对象时 end_time = time.time() + timeout - if mode == 'del': - while True: + while time.time() < end_time: + if mode == 'del': try: loc_or_ele.is_enabled() except: return True - if time.time() > end_time: - return False - elif mode == 'visible': - pass - elif mode == 'invisible': - pass + elif mode == 'visible' and loc_or_ele.is_displayed(): + return True + elif mode == 'invisible' and not loc_or_ele.is_displayed(): + return True + return False else: # 当传入参数是控制字符串或元组时 - if mode == 'del': - return WebDriverWait(self.driver, timeout).until_not(ec.presence_of_element_located(*loc_or_ele)) - elif mode == 'visible': - return WebDriverWait(self.driver, timeout).until(ec.visibility_of_element_located(*loc_or_ele)) - elif mode == 'invisible': - return WebDriverWait(self.driver, timeout).until_not(ec.visibility_of_element_located(*loc_or_ele)) + try: + if mode == 'del': + WebDriverWait(self.driver, timeout).until_not(ec.presence_of_element_located(loc_or_ele)) + elif mode == 'visible': + WebDriverWait(self.driver, timeout).until(ec.visibility_of_element_located(loc_or_ele)) + elif mode == 'invisible': + WebDriverWait(self.driver, timeout).until_not(ec.visibility_of_element_located(loc_or_ele)) + return True + except: + return False def check_page(self) -> Union[bool, None]: """检查页面是否符合预期 @@ -191,16 +204,13 @@ class DriverPage(object): # 根据序号跳转 self.driver.switch_to.frame(loc_or_ele) elif isinstance(loc_or_ele, str): - if loc_or_ele == 'main': - # 跳转到最上级 + if loc_or_ele == 'main': # 跳转到最上级 self.driver.switch_to.default_content() - elif loc_or_ele == 'parent': + elif loc_or_ele == 'parent': # 跳转到上一层 self.driver.switch_to.parent_frame() - elif ':' not in loc_or_ele and '=' not in loc_or_ele: - # 传入id或name + elif ':' not in loc_or_ele and '=' not in loc_or_ele: # 传入id或name self.driver.switch_to.frame(loc_or_ele) - else: - # 传入控制字符串 + else: # 传入控制字符串 ele = self.ele(loc_or_ele) self.driver.switch_to.frame(ele.inner_ele) elif isinstance(loc_or_ele, WebElement):