def verify_exists(element): _run_wait_hook() step_message = 'Verify that the element exists' logger.logger.info(step_message) _capture_or_add_step(step_message, core.settings['screenshot_on_step']) try: webelement = get_driver().find(element, timeout=1) except: raise ElementNotFound('Element {} does not exist'.format(element))
def _find_webelement(root, selector_type, selector_value, element_name, timeout=0, wait_displayed=False, highlight=False): """Finds a web element.""" webelement = None remaining_time = lambda: timeout - (time.time() - start_time) start_time = time.time() while webelement is None: try: if selector_type == 'id': webelement = root.find_element_by_id(selector_value) elif selector_type == 'css': webelement = root.find_element_by_css_selector(selector_value) elif selector_type == 'link_text': webelement = root.find_element_by_link_text(selector_value) elif selector_type == 'partial_link_text': webelement = root.find_element_by_partial_link_text( selector_value) elif selector_type == 'name': webelement = root.find_element_by_name(selector_value) elif selector_type == 'xpath': webelement = root.find_element_by_xpath(selector_value) elif selector_type == 'tag_name': webelement = root.find_element_by_tag_name(selector_value) else: msg = f'Selector {selector_type} is not a valid option' raise IncorrectSelectorType(msg) execution.logger.debug('Element found') except: if remaining_time() <= 0: break else: time.sleep(0.5) execution.logger.debug( 'Element not found yet, remaining time: {:.2f}'.format( remaining_time())) if webelement is None: raise ElementNotFound( f'Element {element_name} not found using selector ' f'{selector_type}:\'{selector_value}\'') else: if wait_displayed: while not webelement.is_displayed() and remaining_time() > 0: execution.logger.debug('Element still not visible, waiting') time.sleep(0.5) if not webelement.is_displayed(): msg = ( f'Timeout waiting for element {element_name} to be displayed, ' f'using selector {selector_type}:\'{selector_value}\'') raise ElementNotDisplayed(msg) return webelement
def _find_webelement(root, selector_type, selector_value, element_name, remaining_time): """Finds a web element.""" webelement = None start_time = time.time() try: if selector_type == 'id': webelement = root.find_element_by_id(selector_value) elif selector_type == 'css': webelement = root.find_element_by_css_selector(selector_value) elif selector_type == 'text': webelement = root.find_element_by_css_selector( "text[{}]".format(selector_value)) elif selector_type == 'link_text': webelement = root.find_element_by_link_text(selector_value) elif selector_type == 'partial_link_text': webelement = root.find_element_by_partial_link_text(selector_value) elif selector_type == 'name': webelement = root.find_element_by_name(selector_value) elif selector_type == 'xpath': webelement = root.find_element_by_xpath(selector_value) elif selector_type == 'tag_name': webelement = root.find_element_by_tag_name(selector_value) else: msg = 'Selector {0} is not a valid option'.format(selector_type) raise IncorrectSelectorType(msg) execution.logger.debug('Element found') except: time.sleep(0.5) end_time = time.time() remaining_time -= end_time - start_time if remaining_time > 0: execution.logger.debug( 'Element not found yet, remaining time: {}'.format( remaining_time)) webelement = _find_webelement(root, selector_type, selector_value, element_name, remaining_time) else: raise ElementNotFound( 'Element {0} not found using selector {1}:\'{2}\''.format( element_name, selector_type, selector_value)) # Use remaining time to wait until element is visible (is_displayed) # TODO add this as a setting remaining_time = remaining_time - (time.time() - start_time) while not webelement.is_displayed() and remaining_time > 0: # Element is not visible yet execution.logger.debug('Element still not visible, waiting') time.sleep(0.5) remaining_time = remaining_time - (time.time() - start_time) if not webelement.is_displayed(): execution.logger.debug('Element not visible, continuing..') return webelement
def verify_exists(element): """Verify that en element exists. Parameters: element : element """ _run_wait_hook() step_message = 'Verify that the element exists' execution.logger.info(step_message) _capture_or_add_step(step_message, execution.settings['screenshot_on_step']) try: webelement = browser.get_browser().find(element, timeout=1) except: raise ElementNotFound('Element {} does not exist'.format(element))
def _find_selenium_element(root, selector_type, selector_value, element_name, remaining_time): webelement = None start_time = time.time() try: if selector_type == 'id': webelement = root.find_element_by_id(selector_value) elif selector_type == 'css': webelement = root.find_element_by_css_selector(selector_value) elif selector_type == 'text': webelement = root.find_element_by_css_selector( "text[{}]".format(selector_value)) elif selector_type == 'link_text': webelement = root.find_element_by_link_text(selector_value) elif selector_type == 'partial_link_text': webelement = root.find_element_by_partial_link_text(selector_value) elif selector_type == 'name': webelement = root.find_element_by_name(selector_value) elif selector_type == 'xpath': webelement = root.find_element_by_xpath(selector_value) elif selector_type == 'tag_name': webelement = root.find_element_by_tag_name(selector_value) else: raise IncorrectSelectorType( 'Selector {0} is not a valid option'.format(selector_type)) except: time.sleep(0.5) end_time = time.time() remaining_time = remaining_time - (end_time - start_time) if remaining_time > 0: webelement = _find_selenium_element(root, selector_type, selector_value, element_name, remaining_time) else: raise ElementNotFound( 'Element {0} not found using selector {1}:\'{2}\''.format( element_name, selector_type, selector_value)) remaining_time = remaining_time - (time.time() - start_time) while not webelement.is_displayed() and remaining_time > 0: # Element is not visible yet time.sleep(0.5) remaining_time = remaining_time - (time.time() - start_time) return webelement