Exemplo n.º 1
0
    def click_element_by_xpath(self, xpath):
        """
        Click an element on the page given its xpath

        :param xpath: the xpath of the element to click
        :type xpath: str
        """
        try:
            self._browser.find_element_by_xpath(xpath).click()
        except NoSuchElementException:
            raise NoSuchElementError(f'No element found for xpath: "{xpath}"')
Exemplo n.º 2
0
    def element_is_selected_by_xpath(self, xpath: str) -> bool:
        """Get whether or not the element specified by the given xpath is selected

        :param xpath: the xpath of the elements of interest
        :type xpath: str
        :return: True if the element is selected, False otherwise
        :rtype: bool
        """
        try:
            return self._browser.find_element_by_xpath(xpath).is_selected()
        except:
            raise NoSuchElementError(f'No elements found for xpath: "{xpath}"')
Exemplo n.º 3
0
    def get_elements_attribute_by_xpath(self, xpath: str,
                                        attribute: str) -> list:
        """
        Get the value of a given attribute for all elements with the given xpath

        :param xpath: the xpath of the elements of interest
        :type xpath: str
        :param attribute: the name of the attribute of interest, e.g. 'value'
        :type attribute: str
        :return: a list of values of the given attribute for each matching element
        :rtype: list
        """
        try:
            elements = self._browser.find_elements_by_xpath(xpath)
            if attribute.lower() == 'text':
                return [element.text for element in elements]
            return [element.get_attribute(attribute) for element in elements]
        except NoSuchElementException:
            raise NoSuchElementError(f'No elements found for xpath: "{xpath}"')
Exemplo n.º 4
0
    def get_element_attribute_by_xpath(self, xpath: str,
                                       attribute: str) -> str:
        """
        Get the value of the given attribute from the element with the given xpath

        :param xpath: the xpath of the element of interest
        :type xpath: str
        :param attribute: the name of the attribute of interest, e.g. 'value'
        :type attribute: str
        :return: the value of the attribute on the element of interest
        :rtype: str
        """
        try:
            if attribute.lower() == 'text':
                return self._browser.find_element_by_xpath(xpath).text
            return self._browser.find_element_by_xpath(xpath).get_attribute(
                attribute)
        except NoSuchElementException:
            raise NoSuchElementError(f'No element found for xpath: "{xpath}"')
Exemplo n.º 5
0
    def execute_script_on_element_by_xpath(self,
                                           script: str,
                                           xpath: str = None):
        """
        Execute the given javascript expression. If xpath of element is provided,
        the element becomes available to use in the script, and can be accessed
        using arguments[0].

        :param script: the javascript to be executed
        :type script: str
        :param xpath: the xpath of the optional element to be passed to the script
        :type xpath: str
        """
        if not xpath:
            self._browser.execute_script(script)
        else:
            try:
                self._browser.execute_script(
                    script, self._browser.find_element_by_xpath(xpath))
            except NoSuchElementException:
                raise NoSuchElementError(
                    f'No elements found for xpath: "{xpath}"')
Exemplo n.º 6
0
    def send_text_to_element_by_xpath(self,
                                      xpath: str,
                                      text: str,
                                      clear: bool = True):
        """
        Send a string to an input field identified by the given xpath

        :param text: the text to send
        :type text: str
        :param xpath: the xpath of the input field to which to send the text
        :type xpath: str
        :param clear: whether or not to clear the field before sending text. If
                      False, text will be appended to any text already present.
        :type clear: bool
        """
        try:
            element = self._browser.find_element_by_xpath(xpath)
            if clear:
                element.clear()
            element.send_keys(text)
        except NoSuchElementException:
            raise NoSuchElementError(f'No element found for xpath: "{xpath}"')
Exemplo n.º 7
0
 def validate_download_modal_is_open(self):
     """Throw an error if the download modal is not open."""
     if not self.is_open:
         raise NoSuchElementError(
             'Insights Download modal box must be open')
Exemplo n.º 8
0
 def switch_to_frame_by_xpath(self, xpath):
     try:
         frame = self._browser.find_element_by_xpath(xpath)
         self._browser.switch_to.frame(frame)
     except NoSuchElementException:
         raise NoSuchElementError(f'No elements found for xpath: "{xpath}"')