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