def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR, timeout=Settings.LARGE_TIMEOUT): element = None start_ms = time.time() * 1000.0 stop_ms = start_ms + (timeout * 1000.0) for x in range(int(timeout * 10)): try: by, selector = _css_change_id(by, selector) element = driver.find_element(by=by, value=selector) if element.is_displayed(): return element else: element = None raise Exception() except Exception: now_ms = time.time() * 1000.0 if now_ms >= stop_ms: break time.sleep(0.1) if not element and by != By.LINK_TEXT: raise ElementNotVisibleException( "Element {%s} was not visible after %s seconds!" % ( selector, timeout)) if not element and by == By.LINK_TEXT: raise ElementNotVisibleException( "Link text {%s} was not visible after %s seconds!" % ( selector, timeout))
def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): """ Searches for the specified element by the given selector. Returns the element object if the element is present and visible on the page. Raises an exception if the element does not appear in the specified timeout. @Params driver - the webdriver object (required) selector - the locator that is used (required) by - the method to search for the locator (Default: By.CSS_SELECTOR) timeout - the time to wait for elements in seconds @Returns A web element object """ element = None for x in range(int(timeout * 10)): try: element = driver.find_element(by=by, value=selector) if element.is_displayed(): return element else: element = None raise Exception() except Exception: time.sleep(0.1) if not element: raise ElementNotVisibleException( "Element %s was not visible in %s seconds!" % (selector, timeout))
def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): """ Searches for the specified element by the given selector. Returns the element object if the element is present and visible on the page. Raises an exception if the element does not appear in the specified timeout. @Params driver - the webdriver object (required) selector - the locator that is used (required) by - the method to search for the locator (Default: By.CSS_SELECTOR) timeout - the time to wait for elements in seconds @Returns A web element object """ element = None start_ms = time.time() * 1000.0 stop_ms = start_ms + (timeout * 1000.0) for x in range(int(timeout * 10)): try: element = driver.find_element(by=by, value=selector) if element.is_displayed(): return element else: element = None raise Exception() except Exception: now_ms = time.time() * 1000.0 if now_ms >= stop_ms: break time.sleep(0.1) plural = "s" if timeout == 1: plural = "" if not element and by != By.LINK_TEXT: raise ElementNotVisibleException( "Element {%s} was not visible after %s second%s!" % (selector, timeout, plural)) if not element and by == By.LINK_TEXT: raise ElementNotVisibleException( "Link text {%s} was not visible after %s second%s!" % (selector, timeout, plural))
def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR, timeout=settings.LARGE_TIMEOUT): """ Searches for the specified element by the given selector. Returns the element object if the text matches exactly with the text in the element, and the text is visible. Raises an exception if the text or element do not appear in the specified timeout. @Params driver - the webdriver object (required) text - the exact text that is expected for the element (required) selector - the locator for identifying the page element (required) by - the type of selector being used (Default: By.CSS_SELECTOR) timeout - the time to wait for elements in seconds @Returns A web element object that contains the text searched for """ element = None start_ms = time.time() * 1000.0 stop_ms = start_ms + (timeout * 1000.0) for x in range(int(timeout * 10)): shared_utils.check_if_time_limit_exceeded() try: element = driver.find_element(by=by, value=selector) if element.is_displayed() and text.strip() == element.text.strip(): return element else: element = None raise Exception() except Exception: now_ms = time.time() * 1000.0 if now_ms >= stop_ms: break time.sleep(0.1) plural = "s" if timeout == 1: plural = "" if not element: raise ElementNotVisibleException( "Expected exact text {%s} for {%s} was not visible " "after %s second%s!" % (text, selector, timeout, plural))
def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR, timeout=LARGE_TIMEOUT): """ Searches for the specified element by the given selector. Returns the element object if the text is present in the element and visible on the page. Raises an exception if the text or element do not appear in the specified timeout. @Params driver - the webdriver object (required) text - the text that is being searched for in the element (required) selector - the locator that is used (required) by - the method to search for the locator (Default: By.CSS_SELECTOR) timeout - the time to wait for elements in seconds @Returns A web element object that contains the text searched for """ element = None start_ms = time.time() * 1000.0 stop_ms = start_ms + (timeout * 1000.0) for x in range(int(timeout * 10)): try: element = driver.find_element(by=by, value=selector) if element.is_displayed(): if text in element.text: return element else: element = None raise Exception() except Exception: now_ms = time.time() * 1000.0 if now_ms >= stop_ms: break time.sleep(0.1) if not element: raise ElementNotVisibleException( "Expected text {%s} for {%s} was not visible after %s seconds!" % (text, selector, timeout))
def wait_for_element_visible(self,type,value,timeout=MAXTIME): """ 等待元素,如果该元素在页面上存在且可见。返回element """ element = None start_time = time.time() wait_time = lambda: timeout - (time.time() - start_time) while element is None: try: element = self._find_element(type,value) if element.is_displayed(): return element else: element = None except: if wait_time() < 0: # 触发跳出循环条件 break else: time.sleep(0.5) logger.error("元素还没有找到,剩余查找时间{:.2f}".format(wait_time())) if not element: raise ElementNotVisibleException("元素等待{time}秒后,依然无法可见".format(time=time))