title: Selenium 2 StaleElementReferenceException When Using DropDownList with AutoPostBack With InternetExplorerDriver
body: <p>I am using Selenium 2 to test an asp.net web forms page using the InternetExplorerDriver and am encountering a StaleElementReferenceException. The page contains a (auto-postback) drop down list, which I am selecting different values from.</p>
  
  <p>Example code:</p>
  
  <p>Page:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication.WebForm1" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
    &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
        &lt;asp:DropDownList ID="ddl" runat="server" AutoPostBack="true"&gt;
          &lt;asp:ListItem Text="one"&gt;&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Text="two"&gt;&lt;/asp:ListItem&gt;
        &lt;/asp:DropDownList&gt;
      &lt;/div&gt;
      &lt;/form&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>(The code-behind file contains nothing more than the Visual Studio auto-created stuff.)</p>
  
  <p>Test fixture code:</p>
  
  <pre><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/&lt;my-virtual-directory-name&gt;/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
      }
    }
  }
  </code></pre>
  
  <p>When I run the test I get the following error:</p>
  
  <pre><code>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.&lt;&gt;c__DisplayClasse.&lt;XPath&gt;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 
  </code></pre>
  
  <p>If I change the test to use a ChromeDriver then the test passes. It seems to me like this means it is either a problem with the InternetExplorerDriver or the Internet Explorer browser itself. Does anybody know which and if there's anything I can do to get round this (the site will be used in IE by the end users, so changing browsers isn't possible, unfortunately)?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> The current work-around that I am using is to put a <code>Thread.Sleep()</code> after the list has been selected; this works but obviously isn't an ideal solution.</p>