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.
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
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.
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.
As you want to pick up the text property once the
WebElement
is no longer stale, you can use the following :From the documentation:
Staleness of:
Element to be clickable:
As you can see, both use the same method is_enabled() in order to perform the check.