StaleElementReferenceException on getText() Method

560 views
Skip to first unread message

Gowtham

unread,
Oct 20, 2009, 5:36:18 PM10/20/09
to webdriver
Hi All,
I am running into the following exception

Following is the sequence of actions that i perform

1) I instantiate a WebElement and then perform a mouse Click by
actually simulating a mouseMove and mousePress functions in the
java.awt.Robot class.

2) For the screen coordinates i typecast this WebElement to Locatable
and invoke the etLocationOnScreenOnceScrolledIntoView() function

3) After this the mouse gets moved to the particular location and
clicks on it.

4) This triggers a AJAX request and hence i introduced a sleep for 10
Seconds

5) The CSS of the elements get modified after the Ajax response.

6) Now when i try to call the getText() on the element, it throws the
following exception

Here is the stack trace
*************************************************************************************************
org.openqa.selenium.StaleElementReferenceException: Element is
obsolete
System info: os.name: 'Linux', os.arch: 'i386', os.version:
'2.6.18-53.1.4.el5PAE', java.version: '1.6.0_11'
Driver info: driver.version: firefox
at org.openqa.selenium.firefox.Response.ifNecessaryThrow
(Response.java:91)
at org.openqa.selenium.firefox.FirefoxDriver.sendMessage
(FirefoxDriver.java:284)
at org.openqa.selenium.firefox.FirefoxWebElement.sendMessage
(FirefoxWebElement.java:277)
at org.openqa.selenium.firefox.FirefoxWebElement.getText
(FirefoxWebElement.java:121)
at apps.webdriver.testWater.TestWaterWD.main(TestWaterWD.java:111)
****************************************************************************************************

Can someone clarify as to when these sort of staleReferenceExceptions
occur. Also any tips on how to avoid them in future



Thanks
Gowtham S


Andrian Kurniady

unread,
Oct 21, 2009, 2:44:42 AM10/21/09
to webd...@googlegroups.com
Hi Gowtham,

I believe StaleElementException is thrown in the cases when you're
trying to do operations on a webelement which is no longer attached to
the root DOM tree of the page. This means the element may have been
"replaced" or similar. In that case, it is advisable to look up for
the element again right before your getText() call to grab a reference
to the new element.

Hope it helps.

-Kurniady

Gowtham Sridharan

unread,
Oct 21, 2009, 12:24:16 PM10/21/09
to webd...@googlegroups.com
Hi Kurniady

Thanks for the quick response.

I have the following clarifications.

1) In all ajax calls there is a possibility of this exception, which means when i use the page Object pattern, i would need to refresh the element(Ajax modified elements) references 

2) Is there any work around instead of refreshing the element reference each and every time when i do some operations, or is it specific to the getText() method alone.


--
Gowtham Sridharan
Carnegie Mellon University


Simon Stewart

unread,
Oct 22, 2009, 3:12:57 AM10/22/09
to webd...@googlegroups.com
Hi,

If you're using the PageObject pattern, you may want to take a look at
the "@FindBy" annotation[1] That will take care of the lookup for you,
and, unless you ask to cache the instance, it'll do a look up on
demand.

Regards,

Simon

[1] http://selenium.googlecode.com/svn/webdriver/javadoc/org/openqa/selenium/support/FindBy.html
Reply all
Reply to author
Forward
0 new messages