Esempio n. 1
0
 def clear(self, locator):
     '''清空输入框文本'''
     ele = self.find(locator)
     if ele.is_displayed():
         ele.clear()
     else:
         raise ElementNotVisibleException(self.log.info("元素不可见或者不唯一"))
Esempio n. 2
0
    def find_element(self, parent, css_selector, force_find=False):
        element = None
        command = "return getObject('{attr}', arguments[0]);".format(attr=css_selector)
        if self.__implicit_wait > 0:
            print(element)
            time.sleep(self.__implicit_wait)
            element = self.executor_get_object(command, parent)

        if self.__explicit_wait > 0:
            element = self.executor_get_object(command, parent)
            print(element)
            count = 0
            while count < self.__explicit_wait and element is None:
                time.sleep(self.__polling_time)
                element = self.executor_get_object(command, parent)
                count = count + 1

        if self.__implicit_wait == 0 and self.__implicit_wait == 0:
            element = self.executor_get_object(command, parent)

        if force_find is False:
            if element is None or self.is_present(element) is False:
                raise ElementNotVisibleException("Element with CSS " + css_selector + " is not present on screen")

        return element
Esempio n. 3
0
 def input(self, locator, text=""):
     '''写入文本'''
     ele = self.find(locator)
     if ele.is_displayed():
         ele.send_keys(text)
     else:
         raise ElementNotVisibleException(self.log.info("元素不可见或者不唯一无法输入"))
Esempio n. 4
0
 def send(self, locator, text=''):
     '''发送文本'''
     ele = self.find(locator)
     if ele.is_displayed():
         ele.send_keys(text)
     else:
         raise ElementNotVisibleException("元素不可见或者不唯一无法输入,解决办法:定位唯一元素,或先让元素可见,或者用js输入")
Esempio n. 5
0
    def find_element(self, by=By.ID, value=None, message='', timeout=None):
        """This function is called by all find_element_by_*().
           This implementation adds a timeout to search for Webelements."""
        # Do not call lambda browser : browser.find_element -> infinite recursion
        if timeout is None:
            timeout = world.timeout

        try:
            WebDriverWait(self,
                          timeout).until(lambda browser: webdriver.Firefox.
                                         find_element(self, by, value))
        except TimeoutException:
            raise NoSuchElementException('%s: %s' % (value, message))

        element = webdriver.Firefox.find_element(self, by, value)

        try:
            WebDriverWait(
                self,
                timeout).until(lambda browser: webdriver.Firefox.find_element(
                    self, by, value).is_displayed())
        except TimeoutException:
            raise ElementNotVisibleException('%s: %s' % (value, message))

        return element
 def navigate_window_air_conditioners_page(self):
     """Navigate to "TV & Appliances - AirConditioners > Window AC’s" page is displayed
     Parameters:
         None
     Returns:
         None
     Raises:
         ElementNotVisibleException
     """
     if not self.wait.until(
             EC.visibility_of_element_located(
                 (By.CSS_SELECTOR,
                  self.locator.FLIPKART_TVANDAPPLIANCE_LINK_BY_CSS))):
         raise ElementNotVisibleException(
             'Unable to find "TV & Appliances - AirConditioners > Window AC’s" page.'
         )
     else:
         self.tv_appliances_tab = self.driver.find_element_by_css_selector(
             self.locator.FLIPKART_TVANDAPPLIANCE_LINK_BY_CSS)
         self.action.move_to_element(self.tv_appliances_tab).perform()
         self.wait.until(
             EC.visibility_of_element_located(
                 (By.CSS_SELECTOR,
                  self.locator.FLIPKART_WINDOWAC_LINK_BY_CSS)))
         self.window_ac_link = self.driver.find_element_by_css_selector(
             self.locator.FLIPKART_WINDOWAC_LINK_BY_CSS)
         self.action.move_to_element(self.window_ac_link).perform()
         self.window_ac_link.click()
 def print_item_delivery_msg(self, item_no):
     """ Print message of item delivery
     Parameters:
         item_no: index of item from list of items
     Returns:
         None
     Raises:
         None
     """
     if not self.wait.until(
             EC.presence_of_element_located(
                 (By.CSS_SELECTOR,
                  self.locator.FLIPKART_CART_PAGE_ITEM_NAME_BY_CSS
                  ))) or not self.wait.until(
                      EC.presence_of_element_located(
                          (By.CSS_SELECTOR,
                           self.locator.DELIVERY_PINCODE_DROPDOWN_BY_CSS))):
         raise ElementNotVisibleException(
             'Unable to find item details on Flipkart cart page')
     else:
         self.product_name = self.driver.find_elements_by_css_selector(
             self.locator.FLIPKART_CART_PAGE_ITEM_NAME_BY_CSS)
         self.product_delivery_details = self.driver.find_elements_by_css_selector(
             self.locator.DELIVERY_AVAILABLE_DETAILS_BY_CSS)
         print('Delivery details of item no. {} is'.format(item_no))
         print('Name: ', self.product_name[item_no - 1].text)
         print(
             'Delivery Details: ',
             self.product_delivery_details[item_no - 1].text.split('|')[0])
Esempio n. 8
0
def wait(self, displayed=True, enabled=True, timeout=None):
    """
    Wait until expected conditions.

    :Args:
        - displayed (bool - default: True): Wait until the element is displayed.
        - enabled  (bool - default: True): Wait until the element is enabled.
        - timeout  (bool - default: None): Timeout in seconds, implicit timeout if None.

    :Returns:
        The element itself.

    :Raises:
         - ElementNotVisibleException - if element is not visible.
         - ElementNotInteractableException - if element is not enabled.
         - NoSuchElementException - if expected conditions not satisfied.
    """
    wait_timer = ExplicitWait(timeout) if timeout is not None else ImplicitWait()
    wait_timer.start()

    once = True
    while (not wait_timer.max_time_exceeded) or once:
        once = False
        if ((displayed is None) or (displayed == self.is_displayed())) and (
            (enabled is None) or (enabled == self.is_enabled())
        ):
            return self
        time.sleep(0.2)

    if displayed:
        raise ElementNotVisibleException()
    elif enabled:
        raise ElementNotInteractableException()
    else:
        raise NoSuchElementException()
Esempio n. 9
0
 def click(self, locator):
     """点击元素"""
     ele = self.find(locator)
     if ele.is_displayed():
         ele.click()
     else:
         raise ElementNotVisibleException("元素不可见或者不唯一无法点击,解决办法:定位唯一元素,或先让元素可见,或者用js点击")
Esempio n. 10
0
 def click(self, locator):
     '''点击元素'''
     ele = self.find(locator)
     if ele.is_displayed():
         ele.click()
     else:
         raise ElementNotVisibleException(self.log.info("元素不可见或者不唯一无法点击"))
Esempio n. 11
0
    def verify_avalibility_of_items_by_pincode(self, pincode):
        """ Verify availibility of item on specific pincode
        Parameters:
            pincode: pincode of address
        Returns:
            None
        Raises:
            None
        """
        if not self.wait.until(
                EC.presence_of_element_located(
                    (By.CSS_SELECTOR,
                     self.locator.DELIVERY_PINCODE_TXTBOX_BY_CSS))):
            raise ElementNotVisibleException(
                'Unable to find text box to check avalability of item by pincode'
            )
        else:
            self.pin_code_check_txtbox = self.driver.find_element_by_css_selector(
                self.locator.DELIVERY_PINCODE_TXTBOX_BY_CSS)
            self.pin_code_check_txtbox.clear()
            self.pin_code_check_txtbox.send_keys(pincode)

            self.wait.until(
                EC.element_to_be_clickable(
                    (By.CSS_SELECTOR,
                     self.locator.DELIVERY_PINCODE_TXTBOX_BY_CSS)))
            self.pin_code_check_txt = self.driver.find_element_by_css_selector(
                self.locator.DELIVERY_PINCODE_CHECK_BY_CSS)
            self.pin_code_check_txt.click()
Esempio n. 12
0
def handle_same_page_username_password(driver, email, password):
    email_input = driver.find_element_by_id("ius-userid")
    if not email_input.is_displayed():
        raise ElementNotVisibleException()
    email_input.clear()  # clear email and user specified email
    email_input.send_keys(email)
    driver.find_element_by_id("ius-password").send_keys(password)
    driver.find_element_by_id("ius-sign-in-submit-btn").submit()
Esempio n. 13
0
 def _wait_until_displayed(self, timeout, interval):
     try:
         WebDriverWait(self._wrapped, timeout, interval).until(lambda d: d.is_displayed())
     except TimeoutException:
         template = ("Waited for element to be displayed for {sec} seconds, ",
                     "but <{target} ...> was not displayed:: <{dumped}>")
         msg = "".join(template).format(sec=timeout, target=self._wrapped.tag_name, dumped=self._dump())
         raise ElementNotVisibleException(msg)
Esempio n. 14
0
 def wait_until_element_is_presented(self, element):
     #@param element - Selenium element, Selenium object
     '''method to wait element to be presented on the page'''
     try:
         el = WebDriverWait(self.driver, 10).until(
             EC.presence_of_element_located(element))
     finally:
         ElementNotVisibleException()
Esempio n. 15
0
 def wait_until_displayed(self, locator, seconds_to_wait=DEFAULT_TIMEOUT):
     try:
         WebDriverWait(self.driver, seconds_to_wait).unparsedEntityDecl(
             ec.visibility_of_element_located(locator))
     except TimeoutException:
         if self.driver.find_element(*locator):
             raise ElementNotVisibleException('Element ' + str(locator) +
                                              ' exists but is not visible')
     return True
Esempio n. 16
0
 def get_calendar_date_picker(self, seconds_to_wait=DEFAULT_TIMEOUT):
     try:
         WebDriverWait(self.driver, seconds_to_wait).\
             until(ec.visibility_of_element_located(self.calendar_date_picker))
     except TimeoutException:
         raise ElementNotVisibleException(
             f"Element '{self.calendar_date_picker}' exists, but are not visible"
         )
     date_picker = self.driver.find_element(*self.calendar_date_picker)
     return date_picker
Esempio n. 17
0
 def wait_until_displayed(self, seconds_to_wait=DEFAULT_TIMEOUT):
     """ Wait for an element to appear, and raise the correct exception if it doesn't """
     try:
         WebDriverWait(self.driver, seconds_to_wait).until(
             ec.visibility_of_element_located(self.locator))
     except TimeoutException:
         if self.driver.find_element(*self.locator):
             raise ElementNotVisibleException(
                 f"Element '{self.locator}' exists but is not visible")
     return True
Esempio n. 18
0
 def send(self, locator, text=""):
     """发送文本"""
     ele = self.find(locator)
     #print(ele)
     #is_displayed():判断元素是否显示 html代码的存在
     #is_selected():判断元素是否选中状态
     if ele.is_displayed():
         ele.send_keys(text)
     else:
         raise ElementNotVisibleException("元素不可见或者不唯一无法输入,解决办法:定位唯一元素,或先让元素可见,或者用js输入")
 def switch_to_frame(self):
     try:
         frame = self.driver.find_element(*self.locators.FRAME)
     except NoSuchElementException:
         raise NoSuchElementException(
             "The frame is not present on the page!")
     if frame.is_displayed():
         self.driver.switch_to.frame(frame)
     else:
         raise ElementNotVisibleException("The frame is not displayed!")
Esempio n. 20
0
 def should_be_visible(self, timeout=10.0):
     """
     Checks if the element is visible and raises ElementNotVisibleException if it is not after the set timeout.
     This method should be used when the test is supposed to fail in case the element is not visible.
     :param timeout: Timeout value in seconds.
     :return: None.
     """
     if not self.wait_until_visible(timeout):
         raise ElementNotVisibleException(
             f'Element was supposed to be visible in {timeout} seconds but was not: {self.xpath}'
         )
Esempio n. 21
0
 def clear(self, locator):
     """
     清空输入框文本
     :param locator:
     :return:
     """
     ele = self.find_element(locator)
     if ele.is_displayed():
         ele.clear()
     else:
         raise ElementNotVisibleException(log.info("元素不可见或者不唯一"))
Esempio n. 22
0
 def get_attribute(self, attribute, seconds_to_wait=DEFAULT_TIMEOUT):
     """ Return the value of attribute if element has such an attribute
         Return None if element does not have that attribute does not
         Return an exception if the element could not be located"""
     try:
         WebDriverWait(self.driver, seconds_to_wait).until(
             ec.visibility_of_element_located(self.locator))
     except TimeoutException:
         if self.driver.find_element(*self.locator):
             raise ElementNotVisibleException(
                 f"Element '{self.locator}' exists but is not visible")
     self.driver.find_element(*self.locator).get_attribute(attribute)
Esempio n. 23
0
    def is_display_timeout(self, element):
        """
        在指定时间内,轮询元素是否显示
        :param element: 元素对象
        :param timeSes: 轮询时间
        :return: bool
        """

        if element.is_displayed() and element.is_enabled():
            return True
        else:
            raise ElementNotVisibleException(msg='当前元素未显示或置灰状态')
Esempio n. 24
0
 def click(self, locator):
     """
     点击元素
     :param locator:
     :return:
     """
     ele = self.find_element(locator)
     if ele.is_displayed():
         ele.click()
     else:
         raise ElementNotVisibleException(
             log.info("点击元素:{}元素不可见或者不唯一无法点击".format(locator)))
Esempio n. 25
0
 def get_month_year(self, seconds_to_wait=DEFAULT_TIMEOUT):
     """
     Returns current month
     :param seconds_to_wait: set to default timeout
     :return: text of month year heading element
     """
     try:
         WebDriverWait(self.driver, seconds_to_wait).until(
             ec.visibility_of_element_located(self.month_heading))
     except TimeoutException:
         raise ElementNotVisibleException(
             f"Element '{self.month_heading}' exists, but is not visible")
     month_year = self.driver.find_element(*self.month_heading)
     return month_year.text
Esempio n. 26
0
 def scroll_into_viewport(self, seconds_to_wait=DEFAULT_TIMEOUT):
     """ Scroll the center of the element into view, if it's not already within view """
     element = self.driver.find_element(*self.locator)
     element_in_view = ec_in_viewport(self.locator)
     if not element_in_view(self.driver):
         self.driver.execute_script(
             "arguments[0].scrollIntoView({block: \"center\"});", element)
         try:
             WebDriverWait(self.driver, seconds_to_wait,
                           poll_frequency=0.1).until(element_in_view)
         except TimeoutException:
             raise ElementNotVisibleException(
                 f"Element '{self.locator}' exists, but cannot be scrolled into view"
             )
Esempio n. 27
0
 def click(self):
     """
     Waits until the element is visible and then clicks on it. Raises ElementNotVisibleException if the element
     is not visible or WebDriverException in case of a different failure reason.
     :return: None.
     """
     try:
         if self.wait_until_visible():
             self.driver.find_element(By.XPATH, self.xpath).click()
         else:
             raise ElementNotVisibleException(
                 f'Element cannot be clicked, because it was not visible: {self.xpath}'
             )
     except WebDriverException:
         raise WebDriverException(
             f'Element cannot be clicked: {self.xpath}')
Esempio n. 28
0
def waitUtilPageLoaded(driver, count):
    """Wait until page loaded.

    Args:
        driver: the webdriver object of this class
    Raises:
        ElementNotVisibleException: Could not load full page in given count-down
    Returns:
        None

    """
    while count:
        count -= 1
        if driver.find_element_by_class_name("banner_copyright"):
            return
    raise ElementNotVisibleException("Could not load the full page!")
Esempio n. 29
0
def handle_different_page_username_password(driver, email, password):
    try:
        email_input = driver.find_element_by_id("ius-identifier")
        if not email_input.is_displayed():
            raise ElementNotVisibleException()
        email_input.clear()  # clear email and use specified email
        email_input.send_keys(email)
        driver.find_element_by_id("ius-sign-in-submit-btn").click()
    # click on username if on the saved usernames page
    except (ElementNotInteractableException, ElementNotVisibleException):
        username_elements = driver.find_elements_by_class_name(
            "ius-option-username")
        for username_element in username_elements:
            if username_element.text == email:
                username_element.click()
                break
Esempio n. 30
0
 def input_text(self, locator, text):
     """
     文本框输入,写入文本
     :param locator:
     :param text:
     :return:
     """
     ele = self.find_element(locator)
     # is_displayed()用于判断某个元素是否存在页面上
     # 这里的存在不是肉眼看到的存在,而是HTML代码的存在。某些情况元素的visibility为hidden或者display
     # 属性为None,我们在页面看不到但是实际是存在页面的一些元素
     if ele.is_displayed():
         ele.send_keys(text)
     else:
         raise ElementNotVisibleException(
             log.info("输入文本:{}元素不可见或者不唯一无法输入".format(text)))