detect stale element using selenium 2

1,287 views
Skip to first unread message

Michael Mata

unread,
Dec 19, 2011, 5:44:03 PM12/19/11
to Selenium Users
Using selenium 2, is there a way to test if an element is stale?

Suppose I initiate a transition from one page to another (A -> B). I
then select element X and test it. Suppose element X exists on both A
and B.

Intermittently, X is selected from A before the page transition
happens and not tested until after going to B, raising a
StaleElementReferenceException. It's easy to check for this condition:

try:
visit_B()
element = driver.find_element_by_id('X') # Whoops, we're still on A
element.click()
except StaleElementReferenceException:
element = driver.find_element_by_id('X') # Now we're on B
element.click()

But I'd rather

element = driver.find_element_by_id('X') # Get the elment on A
visit_B()
WebDriverWait(element, 2).until(lamba element: is_stale(element)
element = driver.find_element_by_id('X') # Get element on B

Luke Inman-Semerau

unread,
Dec 27, 2011, 2:08:36 PM12/27/11
to seleniu...@googlegroups.com
Yes... Java has some helper methods to make that line look like:
new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(element));

Looks like you're using python, we haven't written those helper conditions yet, so you'll have to write your own (like this):

def stalenessOf(driver, element):



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


Luke Inman-Semerau

unread,
Dec 27, 2011, 2:12:26 PM12/27/11
to seleniu...@googlegroups.com
opps, sorry didn't mean to hit send....

def stalenessOf(driver, element):
  try:
    element.is_enabled()
    return False
  except:
    return True 

WebDriverWait(driver, 2).until(lambda d: stalenessOf(d, element))


^ not sure the scoping will work appropriately though.

Gulshan Saini

unread,
Dec 28, 2011, 3:39:44 AM12/28/11
to seleniu...@googlegroups.com
HI Michale/Luke,

Can you please help me understand what the issue is. What do you mean "test if an element is stale"?

Thanks
G

Luke Inman-Semerau

unread,
Dec 28, 2011, 9:23:47 AM12/28/11
to seleniu...@googlegroups.com
An element is considered stale if it was found on a page and then the page was reloaded or the element was removed from the DOM.

A practical use of why you would use / want this is if you find an element, then click it, if webdriver returns immediately before the page has refreshed, you can wait until the element is stale to know that the new page has started loading. This is particularly useful when the new page has all the same id's and class names as the previous page, making explicit or implicit waits useless (as they will find the element on the existing page and not the new one being loaded).

-Luke
> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/voCDxd0ZI6IJ.

Gulshan Saini

unread,
Jan 2, 2012, 6:19:19 AM1/2/12
to seleniu...@googlegroups.com
Thank you Luke - It was really helpful

Regards
Gulshan

Michael Mata

unread,
Jan 3, 2012, 1:44:19 PM1/3/12
to Selenium Users
Thanks Luke. I ended up with a similar solution.

def is_stale(element):
try:
element.text
return False
except StaleElementReferenceException:
return True

On Dec 27 2011, 11:12 am, Luke Inman-Semerau <luke.seme...@gmail.com>
wrote:
> opps, sorry didn't mean to hit send....
>
> def stalenessOf(driver, element):
>   try:
>     element.is_enabled()
>     return False
>   except:
>     return True
>
> WebDriverWait(driver, 2).until(lambda d: stalenessOf(d, element))
>
> ^ not sure the scoping will work appropriately though.
>
> On Tue, Dec 27, 2011 at 11:08 AM, Luke Inman-Semerau <luke.seme...@gmail.com
>
>
>
>
>
>
>
> > wrote:
> > Yes... Java has some helper methods to make that line look like:
> > new WebDriverWait(driver,
> > 10).until(ExpectedConditions.stalenessOf(element));
>
> > Looks like you're using python, we haven't written those helper conditions
> > yet, so you'll have to write your own (like this):
>
> > def stalenessOf(driver, element):
>
Reply all
Reply to author
Forward
0 new messages