Selenium 2 StaleElementReferenceException when using DropDownList with AutoPostBack using InternetExplorerDriver

I use Selenium 2 to test my asp.net web form page with InternetExplorerDriver and encounter a StaleElementReferenceException. The page contains a drop-down list (auto-postback), from which I select different values.

Code example:

Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"> <asp:ListItem Text="one"></asp:ListItem> <asp:ListItem Text="two"></asp:ListItem> </asp:DropDownList> </div> </form> </body> </html> 

(The code file does not contain anything other than the one created by Visual Studio.)

Test Device Code:

 using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.IE; namespace IntegrationTests { [TestFixture] public class WebForm1TestFixture { [Test] public void ShouldSelectItemOneThenItemTwo() { IWebDriver driver = new InternetExplorerDriver(); // Using ChromeDriver causes test to pass... driver.Navigate().GoToUrl("http://localhost/<my-virtual-directory-name>/WebForm1.aspx"); IWebElement list = driver.FindElement(By.Id("ddl")); IWebElement itemOne = list.FindElement(By.XPath("option[1]")); itemOne.Select(); list = driver.FindElement(By.Id("ddl")); IWebElement itemTwo = list.FindElement(By.XPath("option[2]")); itemTwo.Select(); list = driver.FindElement(By.Id("ddl")); itemOne = list.FindElement(By.XPath("option[1]"));// This line causes the StaleElementReferenceException to occur itemOne.Select(); // Some assertion would go here } } } 

When I run the test, I get the following error:

 OpenQA.Selenium.StaleElementReferenceException: Element is no longer valid at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in e:\Projects\WebDriver\trunk\remote\client\src\csharp\webdriver-remote-client\RemoteWebDriver.cs: line 883 at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand driverCommandToExecute, Dictionary`2 parameters) in e:\Projects\WebDriver\trunk\remote\client\src\csharp\webdriver-remote-client\RemoteWebDriver.cs: line 727 at OpenQA.Selenium.Remote.RemoteWebElement.FindElement(String mechanism, String value) in e:\Projects\WebDriver\trunk\remote\client\src\csharp\webdriver-remote-client\RemoteWebElement.cs: line 570 at OpenQA.Selenium.Remote.RemoteWebElement.FindElementByXPath(String xpath) in e:\Projects\WebDriver\trunk\remote\client\src\csharp\webdriver-remote-client\RemoteWebElement.cs: line 458 at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context) in e:\Projects\WebDriver\trunk\common\src\csharp\webdriver-common\By.cs: line 119 at OpenQA.Selenium.By.FindElement(ISearchContext context) in e:\Projects\WebDriver\trunk\common\src\csharp\webdriver-common\By.cs: line 227 at OpenQA.Selenium.Remote.RemoteWebElement.FindElement(By by) in e:\Projects\WebDriver\trunk\remote\client\src\csharp\webdriver-remote-client\RemoteWebElement.cs: line 267 at IntegrationTests.WebForm1TestFixture.ShouldSelectItemOneThenItemTwo() in WebForm1TestFixture.cs: line 25 

If I changed the test to use ChromeDriver, then the test will pass. It seems to me that this is either a problem with InternetExplorerDriver, or Internet Explorer. Does anyone know what and if anything that I can do to get around this (the site will be used by IE by end users, so changing browsers is not possible, unfortunately)?


EDIT: The current work I'm using is to put Thread.Sleep() after selecting a list; this works, but obviously is not an ideal solution.

+4
c # internet-explorer selenium-webdriver
source share
3 answers

Below is a sample that I implemented.

After each item*.Select() I fulfilled the wait:

 IWait<IWebDriver> wait = new MyCustomWebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(driver => <some-condition-to-wait-for>); 

Where <some-condition-to-wait-for> is a way to determine if an item has been selected (for example, by checking that another control on the page has been updated, for example.

 wait.Until(driver => driver.FindElement(By.Id("someLabelId")).Text == "expectedValue")`. 

MyCustomWebDriverWait is a class that implements IWait<IWebDriver> and is almost identical to the WebDriverWait class, only it catches a StaleElementReferenceException as well as NotFoundException (which meant changing the type of lastException from NotFoundException to WebDriverException .

You can read, as I pointed out in this direction, Daniel Wagner Hall to the golen group of the Selenium group here .

+3
source share

The list item may change in the DOM due to auto-retry. Try to cancel the list item every time you select an option. For example.

 IWebElement itemOne = driver.FindElement(By.Id("ddl")).FindElement(By.XPath("option[1]")); itemOne.Select(); IWebElement itemTwo = driver.FindElement(By.Id("ddl")).FindElement(By.XPath("option[1]")); itemTwo.Select(); 
0
source share

I found the driver.refresh() call after going to the page where it worked fine, my code is:

  Pages.Login.Goto(); Browser.Refresh(); <-- refresh the webdriver after going to the page Pages.Login.LogInAsRegularUser(); 
0
source share

All Articles