def get_checkbox_by_locator(locator, anchor): """Get checkbox element. Parameters ---------- locator : str Either text that points to the checkbox or direct xpath to the checkbox. If using direct XPath then add prefix xpath=. anchor : str Using if locator is not an XPath. Returns ------- WebElement """ if locator.startswith("xpath=") or locator.startswith("//"): checkbox_element = element.get_unique_element_by_xpath(locator) # TODO: Check that the element is actually a checkbox else: # No prefix given text_element = text.get_text_using_anchor(locator, anchor) xpath = '//input[@type="checkbox"]|//*[@role="checkbox"]' checkbox_elements = element.get_webelements_in_active_area( xpath, stay_in_current_frame=True) checkbox_element = element.get_closest_element(text_element, checkbox_elements) return checkbox_element, None
def click_header(self, text): """Click header text.""" frame.wait_page_loaded() xpath = ('//ul/li/a[.="{}"]'.format(text)) webelements = element.get_webelements_in_active_area(xpath) if not webelements: raise AssertionError( 'Could not find header with text {}'.format(text)) elif len(webelements) == 1: webelements[0].click() else: raise AssertionError( 'Found {} occurences of headers with text {}'.format( len(webelements), text))
def get_dropdown_element_by_locator(locator, anchor): """Find dropdown element. Parameters ---------- locator : str Text that locates the input field. The input field that is closest to the text is selected. Also one can use xpath by adding xpath= prefix and then the xpath. Error is raised if the xpath matches to multiple elements. anchor : str Text near the input field's locator element. If the page contains many places where the locator is then anchor is used to get the one that is closest to it. """ if locator.startswith("xpath=") or locator.startswith("//"): dropdown_element = element.get_unique_element_by_xpath(locator) else: # Search using text # First we look through all select elements' options, matching locator matches = [] elements = _get_all_dropdown_elements() for dd_element in elements: options = [x.text for x in Select(dd_element).options] if locator in options: logger.debug("Found dropdown with options %s" % options) matches.append(dd_element) if matches: correct_element = text.get_element_using_anchor(matches, anchor) return correct_element # Then we try to find the element using attributes and text dropdown_xpath = ( # pylint: disable=line-too-long '//select[normalize-space(@placeholder)="{0}" or normalize-space(@value)="{0}" or normalize-space(text())="{0}"]' .format(locator)) dropdown_elements = element.get_webelements_in_active_area( dropdown_xpath) if len(dropdown_elements) == 1: dropdown_element = dropdown_elements[0] elif not dropdown_elements: # Find dropdown element using locator locator_element = text.get_text_using_anchor(locator, anchor) dropdown_elements = _get_all_dropdown_elements( stay_in_current_frame=True) dropdown_element = element.get_closest_element( locator_element, dropdown_elements) else: # Found many logger.debug("found many, using anchor") dropdown_element = text.get_element_using_anchor( dropdown_elements, anchor) return dropdown_element
def get_table_element(self, locator, anchor): if util.xpath_validator(locator): table_element = element.get_unique_element_by_xpath(locator) else: # Search using text table_xpath = "//*[text()= '{0}']/ancestor::table".format(locator) table_elements = element.get_webelements_in_active_area( table_xpath) if table_elements and len(table_elements) == 1: table_element = table_elements[0] elif not table_elements: # Find table element using locator locator_element = text.get_text_using_anchor(locator, anchor) table_elements = self._get_all_table_elements() table_element = element.get_closest_element( locator_element, table_elements) else: # Found many table_element = text.get_element_using_anchor( table_elements, anchor) if table_element: return table_element raise QWebElementNotFoundError( 'Table element not found by locator {}'.format(locator))
def get_elements_by_locator_xpath_and_tag_name(locator, index=1, **kwargs): index = int(index) - 1 if 'tag' in kwargs: tag_name = kwargs.get('tag') elif 'parent' in kwargs and kwargs['parent']: tag_name = kwargs['parent'] elif 'child' in kwargs and kwargs['child']: tag_name = kwargs['child'] else: tag_name = 'ul' if 'parent' in kwargs and kwargs['parent']: web_element = element.get_unique_element_by_xpath(locator) css = kwargs.get('parent') web_element = element.get_parent_list_element(web_element, css) if tag_name not in ["ul", "ol", "dl", "UL", "OL", "DL"]: web_element = javascript.execute_javascript( 'return arguments[0].querySelectorAll("{}")'.format( tag_name), web_element) else: web_element = javascript.execute_javascript( 'return arguments[0].closest("{}").querySelectorAll("li, dt, dd")' .format(tag_name), web_element) elif 'child' in kwargs and kwargs['child']: web_element = element.get_unique_element_by_xpath(locator) css = kwargs.get('child') web_element = element.get_element_from_childnodes( web_element, css, dom_traversing=False)[index] if tag_name not in ["ul", "ol", "dl", "UL", "OL", "DL"]: web_element = javascript.execute_javascript( 'return arguments[0].querySelectorAll("{}")'.format( tag_name), web_element) else: web_element = javascript.execute_javascript( 'return arguments[0].closest("{}").querySelectorAll("li, dt, dd")' .format(tag_name), web_element) else: web_element = element.get_webelements_in_active_area(locator) return web_element
def get_input_element_by_locator(locator, anchor, **kwargs): """Find input element. Parameters ---------- locator : str Text that locates the input field. The input field that is closest to the text is selected. Also one can use xpath by adding xpath= prefix and then the xpath. Error is raised if the xpath matches to multiple elements. anchor : str Text near the input field's locator element. If the page contains many places where the locator is then anchor is used to get the one that is closest to it. """ if locator.startswith("xpath=") or locator.startswith( "//") or locator.startswith("(//"): if locator.startswith("xpath="): xpath = locator.split("=", 1)[1] else: xpath = locator input_element = element.get_unique_element_by_xpath(xpath, **kwargs) else: # Search using text input_xpath = CONFIG["MatchingInputElement"].format(locator) input_elements = element.get_webelements_in_active_area( input_xpath, **kwargs) if not input_elements: # Find input element using locator locator_element = text.get_text_using_anchor( locator, anchor, **kwargs) input_elements = _get_all_input_elements() input_element = element.get_closest_element( locator_element, input_elements) elif len(input_elements) == 1: input_element = input_elements[0] # pylint: disable=unsubscriptable-object else: # Found many input_element = text.get_element_using_anchor( input_elements, anchor, **kwargs) return input_element
def _get_contains_text_element(text, **kwargs): xpath = (CONFIG["ContainingTextMatch"].format(text)) return element.get_webelements_in_active_area(xpath, **kwargs)
def _get_all_input_elements(): input_elements = element.get_webelements_in_active_area( CONFIG["AllInputElements"], stay_in_current_frame=True) return input_elements
def _get_all_table_elements(): return element.get_webelements_in_active_area('//table')