Se2b2, Win7, IE8 - org.openqa.selenium.StaleElementReferenceException: Element is no longer valid

3,979 views
Skip to first unread message

Alexei Barantsev

unread,
Feb 17, 2011, 4:32:22 PM2/17/11
to selenium-...@googlegroups.com
Hi,

Here is a piece of code that works in FirefoxDriver and raises StaleElementReferenceException in InternetExplorerDriver.

public class Experiment {

public static void main(String[] args) {
WebDriver browser = new InternetExplorerDriver();
WebElement form = browser.findElements(By.tagName("form")).get(1);
form.findElement(By.name("color")).sendKeys("red");
form.submit();

String res = "";
WebElement table = browser.findElements(By.tagName("table")).get(1);
for (WebElement r : table.findElements(By.tagName("tr"))) {
String name = r.findElement(By.tagName("th")).getText();
String value = r.findElement(By.tagName("td")).getText();
res += name + "=" + value + " ";
}

System.out.println(res);
browser.quit();
}
}

Stacktrace:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with tag name == th (WARNING: The server did not provide any stacktrace information)
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:197)
at org.openqa.selenium.ie.InternetExplorerElement.findElement(InternetExplorerElement.java:40)
at org.openqa.selenium.remote.RemoteWebElement.findElementByTagName(RemoteWebElement.java:189)
at org.openqa.selenium.By$5.findElement(By.java:178)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:116)
at Experiment.main(Experiment.java:20)

There is no JavaScript tricks that can screw DOM, just plain HTML. How can that came?

Regards,
-- 
Alexei Barantsev
Software-Testing.Ru

Alexei Barantsev

unread,
Feb 17, 2011, 4:42:48 PM2/17/11
to selenium-...@googlegroups.com
Oh, my!

While I was writing the post, behavoiur have been changed and I've posted NoSuchElementException report.

But then I've run several more times and now I have again StaleElementReferenceException:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element is no longer valid (WARNING: The server did not provide any stacktrace information)
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:197)
at org.openqa.selenium.ie.InternetExplorerElement.findElement(InternetExplorerElement.java:40)
at org.openqa.selenium.remote.RemoteWebElement.findElementByTagName(RemoteWebElement.java:189)
at org.openqa.selenium.By$5.findElement(By.java:178)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:116)
at Experiment.main(Experiment.java:20)

It seems like a race condition, that produces various flavours of exceptions.

Eran M.

unread,
Feb 17, 2011, 6:45:13 PM2/17/11
to selenium-...@googlegroups.com

The page you pointed is not just static HTML - it invokes a CGI script after submitting the form. It looks like your test sometimes proceeds to iterating over the table elements assuming submit returns after the new page, with the results of the CGI script, loads. While that behaviour is desired, it's hard to guarantee, which is why we recommend checking for specific state of a page after interactions with the page that change it or load a new one.

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

Alexei Barantsev

unread,
Feb 17, 2011, 6:59:29 PM2/17/11
to selenium-...@googlegroups.com
Hi, Eran,

What do you mean by a page state? WebElement presense? Yes, it is present (but detached for some reason).

Whould you please point me to the recommendations you mentioned/
What I can see is documentation of submit method that says it should block until page is loaded:

void submit()
If this current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.

Alexei Barantsev

unread,
Feb 19, 2011, 6:54:26 AM2/19/11
to selenium-...@googlegroups.com
So, friends, could anybody tell me how to make abovementioned piece of code work in InternetExplorerDriver?
What event should I wait for to get rid of StaleElementReferenceException?

There is no problem with FirefoxDriver, it works like a charm.
ChromeDriver works too, with a slight modification to overcome http://code.google.com/p/selenium/issues/detail?id=440

Regards,
Alexei

Patrick Lightbody

unread,
Feb 19, 2011, 8:43:59 PM2/19/11
to selenium-...@googlegroups.com, Eran M.
Eran,
Would love if you could provide some sample code illustrating what you
mean. As embarrassing as it is to admit it, I actually run in to these
kinds of problems quite regularly myself. If I am, I'm sure a lot of
our users are. It's not 100% clear to me what the "WebDriver way" is
for "checking for specific state".

Patrick

Simon Stewart

unread,
Feb 20, 2011, 8:41:18 AM2/20/11
to selenium-...@googlegroups.com, Patrick Lightbody, Eran M.
The code is pretty easy. There are two ways of doing this, either
using the implicit waits or the Wait interface ("explicit waits",
which are as littered with profanity as they could be). Implicit waits
get invoked like so:

driver.manage().timeouts().implicitlyWait();

The implicit waits cause "findElement" to wait up to the expected time
limit for an element to be present before throwing an exception, and
"findElements" to wait until the list of returned elements is not
empty or the time limit is exceeded.

Although implicit waits can make code look clearer, they also hide the
intent of the test. Using the Wait interface is pretty easy:

Wait<WebDriver> wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(elementIsVisible(By.id("fish"));

Where "elementIsVisible" is defined as:

public ExpectedCondition<WebElement> elementIsVisible(
final By locator) {
return new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver driver) {
WebElement e = driver.findElement(locator);
if ((RenderedWebElement) e).isDisplayed()) {
return e;
}
return null;
}
}
}

Simon

Simon Stewart

unread,
Feb 20, 2011, 8:43:03 AM2/20/11
to selenium-...@googlegroups.com
It's possible that the behavior of "submit" has changed when it was
recently reworked in the IE driver. I certainly noticed some increased
flakiness in our own test suites with it, caused by it returning
sooner than it used to.

Feel free to raise a bug about this so it doesn't drop of the radar of
"Things To Check Out"

Simon

Patrick Lightbody

unread,
Feb 20, 2011, 12:27:07 PM2/20/11
to Simon Stewart, selenium-...@googlegroups.com, Eran M.
OK, so that's nothing more than standard WebDriver code I expected. I
was just making sure there wasn't something extra I was missing. I
will say that in the past (haven't tried 2.0b1 or 2.0b2 yet) I would
get StaleStateExceptions through when doing these kinds of waitFor
calls. I found that I would have to wrap the waitFor logic with a
catch of StaleStateException and just ignore it in order to make
things work. Is that normal behavior?

Alexei Barantsev

unread,
Feb 20, 2011, 12:49:03 PM2/20/11
to selenium-...@googlegroups.com, Patrick Lightbody, Eran M.
Hi, Simon,

Such a wait is of no help, so I'll raise a bug report.

I've created a new version (imports skipped):

public class Experiment {

static public ExpectedCondition<WebElement> elementIsVisible(final By locator) {
 return new ExpectedCondition<WebElement>() {
   public WebElement apply(WebDriver driver) {
     WebElement e = driver.findElement(locator);
     if (((RenderedWebElement) e).isDisplayed()) {
       return e;
     }
     return null;
   }
 };
}

public static void main(String[] args) {
WebDriver browser = new InternetExplorerDriver();
WebElement form = browser.findElements(By.tagName("form")).get(1);
form.findElement(By.name("color")).sendKeys("red");
form.submit();
Wait<WebDriver> wait = new WebDriverWait(browser, 5);
WebElement element = wait.until(elementIsVisible(By.xpath("//table//table")));

String res = "";
WebElement table = browser.findElements(By.tagName("table")).get(1);
for (WebElement r : table.findElements(By.tagName("tr"))) {
String name = r.findElement(By.tagName("th")).getText();
String value = r.findElement(By.tagName("td")).getText();
res += name + "=" + value + " ";
}

System.out.println(res);
browser.quit();
}
}

and the exception is still here:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element is no longer valid (WARNING: The server did not provide any stacktrace information)
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_23'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:197)
at org.openqa.selenium.ie.InternetExplorerElement.findElements(InternetExplorerElement.java:47)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:193)
at org.openqa.selenium.By$5.findElements(By.java:171)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:112)
at Experiment.main(Experiment.java:42)

Regards,
Alexei

Alexei Barantsev

unread,
Feb 20, 2011, 12:50:38 PM2/20/11
to selenium-...@googlegroups.com
Yes, I've checked se2b1 -- it works as expected. So it is a new bug of beta2.

Regards,
Alexei

Jim Evans

unread,
Feb 20, 2011, 1:59:02 PM2/20/11
to Selenium Developers
Submit was refactored to use the submit automation atom in certain
cases. This refactor did indeed happen between 2.0b1 and 2.0b2. I'm
sure I missed something in the changes. I have been taking a (IMO)
well-deserved weekend off, but I'll try to evaluate it next week.

--Jim

Simon Stewart

unread,
Feb 22, 2011, 12:50:28 AM2/22/11
to selenium-...@googlegroups.com
Thanks Jim. Hope you enjoyed the weekend off :)

Simon

Rostislav Matl

unread,
Apr 7, 2011, 10:18:49 AM4/7/11
to Selenium Developers
I found that 2.0a6 is remarkably more stable for both FireFox and MSIE
than
newer versions. The beta versions work well for FireFox but behave
kind of
unpredictably in MSIE.

I was not able to move to higher version (I think 2.0a7 had some
problems to start FireFox - I'm not not sure), 2.0b2 threw the
StaleElementReferenceException and 2.0b3 even NullPointerException -
all on the same code
( driver.findElements(By.id("xxx")).isEmpty(); ). I can send
stacktraces - will it help ?

From what I saw I'm not convinced the problem is related to submit.

Dmitry Starchevsky

unread,
Oct 12, 2012, 3:32:58 AM10/12/12
to selenium-...@googlegroups.com
IE works slower that Crhome. Put your code into "try - catch" and
perform findElement again.

2012/10/12 Anand Chaurasia <chauras...@gmail.com>:
> Hi Alexei,
> Did you get solution. I am also getting same error
> "org.openqa.selenium.StaleElementReferenceException: Element is no longer
> valid" with IE. But code is working fine with Chrome browser.
>
> Please help if you got the solution.
>
> Regards
> Anand
>
> --
>
>
Reply all
Reply to author
Forward
0 new messages