def get_element_by_locator_text(locator, anchor="1", index=1, **kwargs): """Find element by it's visible text. Accepted kwargs: parent(tagName):Can be used when target element is some of the locators parent. child(tagName): Find clickable target from locator's child elements. allow_non_existent = True: Function returns immediately if element is not found css=False: Use this to bypass css search when finding elements by visible text """ index = int(index) - 1 try: web_element = get_text_using_anchor(locator, anchor, **kwargs) except (QWebElementNotFoundError, InvalidSelectorException, JavascriptException, WebDriverException): try: web_element = element.get_unique_element_by_xpath(locator) except (QWebElementNotFoundError, InvalidSelectorException, NoSuchFrameException) as e: no_raise = util.par2bool(kwargs.get('allow_non_existent', False)) if no_raise: return None raise QWebElementNotFoundError(e) # pylint: disable=W0707 if web_element: if 'parent' in kwargs and kwargs['parent']: tag_name = kwargs['parent'] web_element = element.get_parent_element(web_element, tag_name) elif 'child' in kwargs and kwargs['child']: tag_name = kwargs['child'] web_element = element.get_element_from_childnodes( web_element, tag_name, dom_traversing=False)[index] if CONFIG['SearchMode']: element.draw_borders(web_element) return web_element raise QWebElementNotFoundError('Element not found')
def get_dd_elements_from_all_documents(locator, anchor, index, **kwargs): if int(index) > 0: index = int(index) - 1 css_selector = CONFIG["CssSelectors"] if not css_selector or locator.startswith('xpath=') or locator.startswith( '//'): select = get_dropdown_element_by_locator(locator, anchor) elif Table.is_table_coordinates(locator): table = Table.ACTIVE_TABLE.update_table() if table is None: raise QWebInstanceDoesNotExistError( 'Table has not been defined with UseTable keyword') locator = table.get_table_cell(locator, anchor) select = element.get_element_from_childnodes( locator, 'select', dom_traversing=False)[index] else: select = get_dropdown_element_by_css_selector(locator, anchor, index, **kwargs) if not select: select = get_dropdown_element_by_locator(locator, anchor) if select: if CONFIG['SearchMode']: element.draw_borders(select) return Select(select) raise QWebElementNotFoundError('No matching elements found')
def get_item_using_anchor(text, anchor, **kwargs): xpath = '//*[@title="{0}" or @alt="{0}" or @data-tooltip="{0}" or ' \ '@tooltip="{0}" or @aria-label="{0}" or @data-icon="{0}"]'.format(text) if CONFIG["CssSelectors"]: web_elements = _get_item_by_css(text, **kwargs) else: web_elements = element.get_webelements(xpath, **kwargs) if web_elements: if CONFIG['SearchMode']: element.draw_borders(_get_correct_element(web_elements, str(anchor))) return _get_correct_element(web_elements, str(anchor)) no_raise = util.par2bool(kwargs.get('allow_non_existent', False)) if no_raise: return None raise QWebElementNotFoundError('Cannot find item for locator {}'.format(text))
def create_list(self, locator, anchor, **kwargs): if locator.startswith('//') or locator.startswith('xpath='): if locator.startswith('xpath='): locator = locator.split("=", 1)[1] web_elements = self.get_elements_by_locator_xpath_and_tag_name( locator, anchor, **kwargs) else: web_elements = self.get_elements_by_locator_text_and_tag_name( locator, anchor, **kwargs) logger.debug('webelems: {}'.format(web_elements)) if web_elements: if CONFIG['SearchMode']: element.draw_borders(web_elements) web_list = self.get_texts(web_elements) return web_list, web_elements raise QWebElementNotFoundError('Suitable elements not found')
def get_table_cell(self, coordinates, anchor, **kwargs): # pylint: disable=unused-argument cell = None try: if '/' in coordinates: cell = self.get_using_text_in_coordinates(coordinates, anchor) else: row, column = self._convert_coordinates(coordinates) try: cell = self.table.find_element( By.XPATH, './/tr[{0}]//td[{1}]'.format(row, column)) except AttributeError as e: logger.debug('exception {}'.format(e)) self.update_table() if cell: if CONFIG['SearchMode']: element.draw_borders(cell) return cell except (StaleElementReferenceException, NoSuchElementException) as e: logger.debug('exception {}'.format(e)) self.update_table() raise QWebElementNotFoundError( 'Cell for coords {} not found after'.format(coordinates))
def from_table_instance(cls, locator, anchor, parent=None, child=None, level=1, index=1): """Create table instance by finding table based on locator Parameters ---------- locator : str Text that locates the table. The table 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 table'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 CONFIG["CssSelectors"] and not util.xpath_validator(locator): table_element = cls.get_table_element_by_css(locator, anchor) else: table_element = cls.get_table_element(cls, locator, anchor) if parent is None and child is None: if CONFIG['SearchMode']: element.draw_borders(table_element) return Table(table_element, locator, anchor, parent, child, level, index) table_element = cls.get_table_by_locator_table(table_element, parent, child, level, index) if CONFIG['SearchMode']: element.draw_borders(table_element) return Table(table_element, locator, anchor, parent, child, level, index)
def get_input_elements_from_all_documents(locator, anchor, timeout, index=1, enable_check=False, **kwargs): # pylint: disable=unused-argument """Function for finding input elements. Parameters ---------- locator : str Label text or attribute that points to the checkbox. anchor : str in case there is duplicates. timeout : str How long we are finding before fail. Default = Search Strategy global default = 10 sec) index : int If table cell contains more than one input elements or if there is some kind of nested structure inside of given input index may needed. Default = 1 (first) enable_check : bool When CSS Selectors are used, we are not return disabled input element always by default. Element is needed with verify input status kw so if enable_check= True, disabled input_element is returned kwargs: limit_traverse : bool If set to false. We are heading up to fifth parent element if needed when finding relative input element for some label text. Returns ------- WebElement """ index = int(index) - 1 css = 'input:not([type="hidden"]):not([type="submit"]):not([type="button"])' \ ':not([type="reset"]):not([type="checkbox"]):not([type="radio"])' \ ':not([aria-hidden="true"]),' \ 'textarea:not([type="hidden"]),[contenteditable="true"]' kwargs['css'] = kwargs.get('css', css) if Table.is_table_coordinates(locator): table = Table.ACTIVE_TABLE.update_table() if table is None: raise QWebInstanceDoesNotExistError( 'Table has not been defined with UseTable keyword') locator_element = table.get_table_cell(locator, anchor) input_element = element.get_element_from_childnodes( locator_element, kwargs['css'], dom_traversing=False) if input_element: return input_element[index] raise QWebElementNotFoundError('No matching table input found') css_selector = CONFIG["CssSelectors"] if not css_selector or locator.startswith('xpath=') or locator.startswith( '//'): input_element = get_input_element_by_locator(locator, anchor, **kwargs) else: logger.debug('Uses CSS-selectors to locate element') input_element = get_input_element_by_css_selector( locator, anchor, index, enable_check, **kwargs) if not input_element: input_element = get_input_element_by_locator( locator, anchor, **kwargs) if input_element: if CONFIG['SearchMode']: element.draw_borders(input_element) return input_element raise QWebElementNotFoundError('No matching input elements found')