c# - Selenium 2 StaleElementReferenceException 使用 DropDownList 与 AutoPostBack 与 InternetExplorerDriver

标签 c# internet-explorer selenium-webdriver

我正在使用 Selenium 2 测试使用 InternetExplorerDriver 的 asp.net Web 表单页面,并且遇到 StaleElementReferenceException。该页面包含一个(自动回发)下拉列表,我从中选择不同的值。

示例代码:

页面:

<%@ 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>

(代码隐藏文件仅包含 Visual Studio 自动创建的内容。)

测试治具代码:

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
    }
  }
}

当我运行测试时,出现以下错误:

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 

如果我将测试更改为使用 ChromeDriver,则测试通过。在我看来,这意味着它要么是 InternetExplorerDriver 的问题,要么是 Internet Explorer 浏览器本身的问题。有谁知道我可以做些什么来解决这个问题(该站点将由最终用户在 IE 中使用,因此无法更改浏览器,不幸的是)?


编辑: 我目前使用的解决方法是在选择列表后放置一个 Thread.Sleep();这可行,但显然不是理想的解决方案。

最佳答案

下面是我最终实现的模式。

在每个item*.Select()之后我已经实现了等待:

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

其中 是一种确定项目选择已完成的方式(例如,通过检查页面上的另一个控件是否已更新,例如

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

MyCustomWebDriverWait是一个实现 IWait<IWebDriver> 的类并且几乎与 WebDriverWait 相同类,只有它捕获 StaleElementReferenceException以及NotFoundException (这意味着将 lastException 的类型从 NotFoundException 更改为 WebDriverException

您可以在 Selenium-users google 组 here 上阅读 Daniel Wagner-Hall 如何向我指出这个方向.

关于c# - Selenium 2 StaleElementReferenceException 使用 DropDownList 与 AutoPostBack 与 InternetExplorerDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116258/

相关文章:

c# - System.NotSupportedException : Unable to find the default constructor. 请提供缺少的构造函数 (xamarin C#)

C# 和 Oracle,登录表单 : Operation is not valid due to the current state of the object

html - 旧版 Internet Explorer 中不显示网站侧边栏

Javascript 适用于 Chrome & Opera & Edge,但不适用于 FireFox 或 IE 11

google-chrome - java.lang.NoSuchMethodError : com. google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;通过 ChromeDriver 启动 Chrome 时

c# - 即使元素存在,ExpectedConditions.ElementIsVisible 也会返回 TimeoutException

javascript - ASP.NET 4.6 WEB API,不提供静态文件(.js、.css) IIS Express Visual Studio

css - 位置绝对和相对 IE7 问题

python-3.x - 单击并使用python分配给变量后如何使用selenium复制元素

c# - 使用 visual studios 2010 的 asp.net mvc4 教程中的 app_data 中未显示数据库文件