def find_children(self, timeout=0): """ get the last element in the tree from the root element :type timeout: int :rtype: list[WebElement] | [] """ try: return self.find_all(Locator.xpath('./*'), timeout) except: return []
def find(self, locator): """find elements using xpath :param locator: ('XPATH', '//a') :type locator: (str, str) :rtype: [] """ if locator[0] in (Locator.BY.CSS, Locator.BY.CSS_SELECTOR): return self.find_by_css(locator[1]) elif locator[0] != Locator.BY.XPATH: locator = Locator.xpath_translator(*locator) return self.tree.xpath(locator[1])
class locator(Locator): # you can use a variety of methods to build locators type_submit = ('type', 'submit') qaviton_logo = ('id', 'site-logo') qaviton_menu_home_button = ('xpath', '//a[text()="LOGIN"]') SIGN_UP_FOR_A_BETA = ('text', 'SIGN UP FOR A BETA') # you can use the Locator directly to build your locator qaviton_email_demo_request = Locator.xpath( '//input[@placeholder="Your Email"]') qaviton_company_demo_request = Locator.xpath( '//input[@placeholder="Company"]') qaviton_name_demo_request = Locator.xpath('//input[@placeholder="Name"]') qaviton_send_demo_request = Locator.tuple( Locator.xpath('//button[@type="submit"]//*[text()="SEND"]'), Locator.index(type_submit, index=0)) # you can use 'tuple' or 'list' to build a redundant locator and support self healing google_search_bar = (('id', 'lst-ib'), ('index', 2, ('xpath', '//input')), ('xpath', '//input[@type="text"][@value=""]')) google_search_button = ('name', 'btnK') google_linkedin_search_result = ('text', 'LinkedIn: Log In or Sign Up') # id/test-id is the best choice for locators. linkedin_reg_firstname = ('id', 'reg-firstname') linkedin_reg_lastname = ('id', 'reg-lastname') linkedin_reg_email = ('id', 'reg-email') linkedin_reg_password = ('id', 'reg-password') linkedin_reg_submit = ('id', 'registration-submit')
def find_last_children(self, timeout=0): """ get the last elements in the tree from the root element :type timeout: int :rtype: list[WebElement] """ locator = Locator.xpath('./*') elements = [] while True: try: elements = self.find_all(locator, timeout) locator = (locator[0], locator[1] + '/*') except: return elements
def find(self, locator: tuple, timeout: int=0, index=0): """find element with locator value :param locator: locate by method like id and value :param timeout: how long to search :param index: parameter for special cases where a list of elements is in the locator, the default element to return is elements[0] :rtype: WebElement """ # use this locate method if timeout is 0 def fast(locate): return self.find_element(*locate) # use this locate method if timeout > 0 def slow(locate): return WebDriverWait(self, timeout).until(presence_of_element_located(locate)) # redundancy if isinstance(locator[0], tuple) or isinstance(locator[0], list): element = None for i in range(len(locator)): try: element = self.find(locator[i], timeout=timeout, index=index) break except Exception as e: if len(locator) == i + 1: raise e return element # find with index if locator[0] == ByExtension.INDEX: return self.find_all(locator[2])[locator[1]] # find with any other strategy by, value = Locator.any(locator) # if element is already found if by == ByExtension.ELEMENTS: return value[index] elif by == ByExtension.ELEMENT: return value # choose finding method if timeout > 0: get = slow else: get = fast # find element return get((by, value))
def find_all(self, locator: tuple, timeout: int=0): """find all elements with locator value :param timeout: how long to search :param locator: locate by method like id and value :rtype: list[WebElement]""" # use this locate method if timeout is 0 def fast(locate): return self.find_elements(*locate) # use this locate method if timeout > 0 def slow(locate): return WebDriverWait(self, timeout).until(presence_of_all_elements_located(locate)) # redundancy if isinstance(locator[0], tuple) or isinstance(locator[0], list): elements = None for i in range(len(locator)): try: elements = self.find_all(locator[i], timeout=timeout) break except Exception as e: if len(locator) == i+1: raise e return elements # find with index if locator[0] == ByExtension.INDEX: return self.find_all(locator[2])[locator[1]] # find with any other strategy by, value = Locator.any(locator) # if element is already found if by == ByExtension.ELEMENTS: return value elif by == ByExtension.ELEMENT: return [value] # choose finding method if timeout > 0: get = slow else: get = fast # find elements return get((by, value))
class locator(Locator): # you can use a variety of methods to build locators type_submit = ('type', 'submit') qaviton_logo = ('id', 'site-logo') qaviton_menu_home_button = ('xpath', '//a[text()="LOGIN"]') SIGN_UP_FOR_A_BETA = ('text', 'SIGN UP FOR A BETA') # you can use the Locator directly to build your locator qaviton_email_demo_request = Locator.xpath('//input[@placeholder="Your Email"]') qaviton_company_demo_request = Locator.xpath('//input[@placeholder="Company"]') qaviton_name_demo_request = Locator.xpath('//input[@placeholder="Name"]') qaviton_send_demo_request = Locator.tuple( Locator.xpath('//button[@type="submit"]//*[text()="SEND"]'), Locator.index(type_submit, index=0)) # you can use 'tuple' or 'list' to build a redundant locator and support self healing google_search_bar = ( ('id', 'lst-ib'), ('index', 2, ('xpath', '//input')), ('xpath', '//input[@type="text"][@value=""]')) google_search_button = ('name', 'btnK') google_linkedin_search_result = ('text', 'LinkedIn: Log In or Sign Up') # id/test-id is the best choice for locators. linkedin_reg_firstname = ('id', 'reg-firstname') linkedin_reg_lastname = ('id', 'reg-lastname') linkedin_reg_email = ('id', 'reg-email') linkedin_reg_password = ('id', 'reg-password') linkedin_reg_submit = ('id', 'registration-submit') # ynet c3_Hor = ('id', 'c3_Hor') evritiframe_1 = ('id', 'evritiframe-1') multiarticles_15 = ('id', 'multiarticles-15') multiarticles_5 = ('id', 'multiarticles-5') close_console = ('id', 'close_console') su_iframe = ('id', 'su_iframe') xButtn = ('id', 'xButtn') console_resize = ('id', 'console_resize') first_title = ('id', 'first_title') arrows = ('id', 'arrows') mainSearchSelectText = ('id', 'mainSearchSelectText') teaserxnet_1 = ('id', 'teaserxnet-1') iframe_container = ('id', 'iframe_container') null = ('id', 'null') ads_300x250_4 = ('id', 'ads.300x250.4')
def click(self, locator=None, timeout=0, index=0): """ click on an element :type locator: tuple(str, str | list[WebElement] | WebElement) :type timeout: int :type index: int :rtype: WebElement """ if locator is None: if timeout == 0: self._execute(Command.CLICK_ELEMENT) else: WebDriverWait(self.parent, timeout)\ .until(EC.element_to_be_clickable(Locator.element(self), index))._execute(Command.CLICK_ELEMENT) return self else: if timeout == 0: element = self.find(locator) element.click() else: element = WebDriverWait(self.parent, timeout).until( EC.element_to_be_clickable(locator, index)) element._execute(Command.CLICK_ELEMENT) return element