Python Selenium: wait until an element is no longer stale?

后端 未结 4 1791
故里飘歌
故里飘歌 2021-01-18 12:19

I have a situation in which I want to wait until an element is no longer STALE i.e. until an elements gets connected to the DOM. Following wait options do not work somehow:<

4条回答
  •  深忆病人
    2021-01-18 13:03

    From the documentation:

    Staleness of:

    class staleness_of(object):
        """ Wait until an element is no longer attached to the DOM.
        element is the element to wait for.
        returns False if the element is still attached to the DOM, true otherwise.
        """
        def __init__(self, element):
            self.element = element
    
        def __call__(self, ignored):
            try:
                # Calling any method forces a staleness check
                self.element.is_enabled()
                return False
            except StaleElementReferenceException:
                return True
    

    Element to be clickable:

    class element_to_be_clickable(object):
        """ An Expectation for checking an element is visible and enabled such that
        you can click it."""
        def __init__(self, locator):
            self.locator = locator
    
        def __call__(self, driver):
            element = visibility_of_element_located(self.locator)(driver)
            if element and element.is_enabled():
                return element
            else:
                return False
    

    As you can see, both use the same method is_enabled() in order to perform the check.

提交回复
热议问题