Exemple #1
0
    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))
Exemple #2
0
    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))