Best Python code snippet using robotframework-appiumlibrary_python
elementfinder.py
Source:elementfinder.py
...61 xpath_tag = tag if tag is not None else '*'62 xpath_constraints = ["@%s='%s'" % (name, constraints[name]) for name in constraints]63 xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs]64 xpath_searchers.extend(65 self._get_attrs_with_url(key_attrs, criteria, browser))66 xpath = "//%s[%s(%s)]" % (67 xpath_tag,68 ' and '.join(xpath_constraints) + ' and ' if len(xpath_constraints) > 0 else '',69 ' or '.join(xpath_searchers))70 return browser.find_elements_by_xpath(xpath)71 # Private72 _key_attrs = {73 None: ['@id', '@name'],74 'a': ['@id', '@name', '@href', 'normalize-space(descendant-or-self::text())'],75 'img': ['@id', '@name', '@src', '@alt'],76 'input': ['@id', '@name', '@value', '@src'],77 'button': ['@id', '@name', '@value', 'normalize-space(descendant-or-self::text())']78 }79 def _get_tag_and_constraints(self, tag):80 if tag is None: return None, {}81 tag = tag.lower()82 constraints = {}83 if tag == 'link':84 tag = 'a'85 elif tag == 'image':86 tag = 'img'87 elif tag == 'list':88 tag = 'select'89 elif tag == 'radio button':90 tag = 'input'91 constraints['type'] = 'radio'92 elif tag == 'checkbox':93 tag = 'input'94 constraints['type'] = 'checkbox'95 elif tag == 'text field':96 tag = 'input'97 constraints['type'] = 'text'98 elif tag == 'file upload':99 tag = 'input'100 constraints['type'] = 'file'101 return tag, constraints102 def _element_matches(self, element, tag, constraints):103 if not element.tag_name.lower() == tag:104 return False105 for name in constraints:106 if not element.get_attribute(name) == constraints[name]:107 return False108 return True109 def _filter_elements(self, elements, tag, constraints):110 if tag is None: return elements111 return filter(112 lambda element: self._element_matches(element, tag, constraints),113 elements)114 def _get_attrs_with_url(self, key_attrs, criteria, browser):115 attrs = []116 url = None117 xpath_url = None118 for attr in ['@src', '@href']:119 if attr in key_attrs:120 if url is None or xpath_url is None:121 url = self._get_base_url(browser) + "/" + criteria122 xpath_url = utils.escape_xpath_value(url)123 attrs.append("%s=%s" % (attr, xpath_url))124 return attrs125 def _get_base_url(self, browser):126 url = browser.get_current_url()127 if '/' in url:128 url = '/'.join(url.split('/')[:-1])...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!