Random OpenQA.Selenium.StaleElementReferenceException inside WebDriverWait.Until

1,454 views
Skip to first unread message

Mark Taylor

unread,
Apr 13, 2011, 4:44:53 AM4/13/11
to seleniu...@googlegroups.com
I have some Selenium 2 tests (written in C#) that choose values from a "select" control. Selection causes a post-back to the server, which updates the state of the page. I am therefore performing a manual wait after choosing a value to wait for the page to be changed. On my development machine this works fine. However, on our CI box, which is a VM and much slower than the dev machines, these tests fail randomly. Sometimes many of them fail, sometimes a few.

The code to perform the wait looks like this:

IWait<IWebDriver> wait = new WebDriverWait(Selenium2Util.Driver, TimeSpan.FromSeconds(10)); 
wait.Until(driver => GetTextBox(driver).Value == "<expected-value>");

(Selenium2Util.Driver is a means of accessing the InternetExplorerDriver instance; unfortunately, we are stuck using the IE driver for this intranet application).

GetTextBox looks like this:

        private static IWebElement GetTextBox(ISearchContext context)
        {
            return context.FindElement(By.Id("<id-of-text-box>"));
        }

The stack trace looks like this:

OpenQA.Selenium.StaleElementReferenceException : Element is no longer valid
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebDriver.cs:line 931
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand driverCommandToExecute, Dictionary`2 parameters) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebDriver.cs:line 779
   at OpenQA.Selenium.Remote.RemoteWebElement.get_Value() in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebElement.cs:line 80
   at <my-code-line>
   at OpenQA.Selenium.Support.UI.WebDriverWait.Until[TResult](Func`2 condition) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\WebDriverWait.cs:line 87


Is there anything I am doing obviously wrong here (e.g. doing the wait wrong)?

Daniel Wagner-Hall

unread,
Apr 13, 2011, 8:19:37 PM4/13/11
to Selenium Users
A StaleElementException generally means the page has changed, so the
IWebElement being referred to is no longer present. If GetTextBox
does any caching, try removing that caching. If not, you're you may
want to create your own Wait class very similar to the one at
https://code.google.com/p/selenium/source/browse/trunk/dotnet/src/WebDriver.Support/UI/WebDriverWait.cs
but which catches StaleElementExceptions as well as
NotFoundExceptions.

If useful, I could probably add a virtual hook into the WebDriverWait
class so you can subclass it and specify which exceptions should be
trapped, without having to create a whole new implementation...

On Apr 13, 9:44 am, Mark Taylor <zoo...@gmail.com> wrote:
> I have some Selenium 2 tests (written in C#) that choose values from a
> "select" control. Selection causes a post-back to the server, which updates
> the state of the page. I am therefore performing a manual wait after
> choosing a value to wait for the page to be changed. On my development
> machine this works fine. However, on our CI box, which is a VM and much
> slower than the dev machines, these tests fail randomly. Sometimes many of
> them fail, sometimes a few.
>
> The code to perform the wait looks like this:
>
> IWait<IWebDriver> wait = new WebDriverWait(Selenium2Util.Driver,
> TimeSpan.FromSeconds(10));
>
> wait.Until(driver => GetTextBox(driver).Value == "<expected-value>");
>
> (*Selenium2Util.Driver* is a means of accessing the *InternetExplorerDriver
> *instance; unfortunately, we are stuck using the IE driver for this intranet
> application).
>
> *GetTextBox* looks like this:
>
>         private static IWebElement GetTextBox(ISearchContext context)
>         {
>             return context.FindElement(By.Id("<id-of-text-box>"));
>         }
>
> The stack trace looks like this:
>
> OpenQA.Selenium.StaleElementReferenceException : Element is no longer valid
>    at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebDriver.cs: line 931
>    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand driverCommandToExecute, Dictionary`2 parameters) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebDriver.cs: line 779
>    at OpenQA.Selenium.Remote.RemoteWebElement.get_Value() in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Remote\RemoteWebElement.cs :line 80
>    at <my-code-line>
>    at OpenQA.Selenium.Support.UI.WebDriverWait.Until[TResult](Func`2 condition) in e:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\WebDriverWait.c s:line 87

Mark Taylor

unread,
Apr 15, 2011, 3:46:35 AM4/15/11
to Selenium Users
Thanks Daniel,

I am not doing any caching - by using the driver passed into the
Until() method, I thought I would avoid getting a
StaleElementException. I shall try creating my own wait class, as per
your suggestion. Being able to specify the exceptions to "ignore"
would be handy though :-)

I will let you know how it goes...

On Apr 13, 8:19 pm, Daniel Wagner-Hall <dawag...@gmail.com> wrote:
> A StaleElementException generally means the page has changed, so the
> IWebElement being referred to is no longer present.  If GetTextBox
> does any caching, try removing that caching.  If not, you're you may
> want to create your own Wait class very similar to the one athttps://code.google.com/p/selenium/source/browse/trunk/dotnet/src/Web...

Mark Taylor

unread,
Apr 15, 2011, 8:01:29 AM4/15/11
to Selenium Users
Ok, so I have tried this and it works; thanks Daniel :-)

Shaun van Halewyn

unread,
Aug 17, 2011, 9:55:30 PM8/17/11
to seleniu...@googlegroups.com
Hi guys,

Can someone give me some more information on how to implement this wait class?  I'm having the same issue but I'm not across the actions needed to resolve it.  I've only just started playing with Selenium recently.

Are we waiting for it to become stale and then grabbing the reference to the object again?

Mark, any chance you want to write a quick blog page on this?

Thanks,
Shaun

Abu Hamzah

unread,
Oct 18, 2012, 9:03:40 PM10/18/12
to seleniu...@googlegroups.com
hey.. i am on the same board now and having the exact same issue... will you tell us how did you able to solve your problem? its old thread but hopefully you will reply.

Ken Pace

unread,
May 30, 2013, 2:21:09 PM5/30/13
to seleniu...@googlegroups.com
Stale elements can be vexing, and there isn't going to be one solution that solves every case.  You'll need to provide more information (code, test flow, etc).


On Thursday, May 30, 2013 2:00:49 AM UTC-7, Raphaël Dhainaut wrote:
I have the same issue too ... it s really frustrating
Someone can be posted his code solution please :)
Reply all
Reply to author
Forward
0 new messages