4

Selenium2を使用してInternetExplorerDriverを使用してasp.netWebフォームページをテストしていますが、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またはInternetExplorerブラウザ自体に問題があることを意味しているように思われます。誰かがこれを回避するために私ができることがあるかどうかを知っていますか(サイトはエンドユーザーによってIEで使用されるため、残念ながらブラウザを変更することはできません)?


編集:私が使用している現在の回避策はThread.Sleep()、リストが選択された後に置くことです。これは機能しますが、明らかに理想的なソリューションではありません。

4

3 に答える 3

3

以下は、私が実装することになったパターンです。

それぞれの後item*.Select()に、待機を実装しました。

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

<some-condition-to-wait-for>は、アイテムの選択が終了したことを確認する方法です(たとえば、ページ上の別のコントロールが更新されたことを確認することによって、たとえば

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

MyCustomWebDriverWaitは、 WebDriverWaitクラスを実装IWait<IWebDriver>し、ほぼ同じクラスですが、キャッチするだけでなく(タイプをからに変更することを意味します) 。StaleElementReferenceExceptionNotFoundExceptionlastExceptionNotFoundExceptionWebDriverException

Selenium-usersgoogleグループのDanielWagner-Hallが、私がこの方向にどのように指摘したかをここで読むことができます

于 2011-04-21T10:16:14.210 に答える
0

自動ポストバックが原因で、リスト要素がDOMで変更されている可能性があります。オプションを選択するたびに、リスト要素を再検索してみてください。例えば

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();
于 2011-03-30T09:57:44.077 に答える
0

ページに移動した後、正常に機能していることがわかりましdriver.refresh()た。私のコードは次のとおりです。

        Pages.Login.Goto();
        Browser.Refresh(); <-- refresh the webdriver after going to the page
        Pages.Login.LogInAsRegularUser();
于 2016-01-21T13:16:57.140 に答える