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:
self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
self.wait.until(EC.presence_of_element_located((By.ID, "elementID")))
Its opposite wait function is present which waits until an element becomes stale, which is:
self.wait.until(EC.staleness_of((By.ID, "elementID")))
But I want it to wait until the element is NO LONGER STALE i.e. until it gets connected to the DOM. How can I achieve this functionality?
EDIT: there is a solution here : here but I am looking for any other better approach if any.
1 Answer. new WebDriverWait(driver, 10). until(ExpectedConditions. invisibilityOfElementLocated(locator));
The Element is not Attached to the DOM If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.
A stale element is an element reference that you have stored that is no longer valid because the page, part of the page, or maybe just the element was refreshed. A simple example
element = driver.find_element_by_id("elementID")
# do something that refreshes the page
element.click()
Here element.click()
will throw a stale element exception because the reference was stored before the refresh but used (clicked) after the refresh. In this case, that reference is no longer valid. Once a reference is stale, it never becomes "unstale"... that reference can never be used again. The only way to fix that is to store the reference again.
NOTE: Your code sample is not correct for .staleness_of()
. It takes a web element reference, not a locator. You need an existing reference to wait for it to go stale. See the docs.
Now to solve the problem... you need to wait for the refresh to complete and then get a new reference.
element = driver.find_element_by_id("elementID")
# do something that refreshes the element
self.wait.until(EC.staleness_of(element))
element = self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
# do something with element
Waiting for the element to become stale waits for the element reference to be lost, which means the element has changed/refreshed. Once we know the element has changed, we can now get a new reference to it. In this case, waiting for the element to become visible. We now have a new reference stored in our variable that we can use without stale element exceptions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With