【问题标题】:Avoiding StaleElementReferenceException when dealing with a dynamic table in Selenium在 Selenium 中处理动态表时避免 StaleElementReferenceException
【发布时间】:2019-04-02 06:07:38
【问题描述】:

我在处理动态表时遇到问题。

这是我们的桌子:

<table class="table" style="min-width: 870px; max-width: 870px">
  <colgroup>
    <col style="width: 30px">
    <col style="width: 200px">
    <col style="width: 80px">
    <col style="width: 70px">
    <col style="width: 200px">
    <col style="width: 50px">
    <col style="width: 50px">
  </colgroup>
  <tbody>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
  </tbody>
  <tfoot>
    <tr>
      <td class="text-right" colspan="3">Media del grupo</td>
      <td class="text-center media" colspan="1">1</td>
      <td class="text-center noeditable" colspan="1"></td>
      <td class="text-center media" colspan="1"></td>
      <td class="text-center media" colspan="1"></td>
    </tr>
  </tfoot>
</table>

每个&lt;tr&gt; 包含以下内容:

<tr>
  <td class="indice">1</td>
  <td id="accion-1-alumno-0" data-tooltip="" class="has-tip titulo" title="">Ap_Alumno_1ESOA_1, Nb_Alumno_1ESOA_1</td>
  <td data-tooltip="" class="has-tip titulo" data-selector="tooltipdtk00g" title="">1ESOA</td>
  <td class="nota relative " style="text-align:center; color: #ed1c24!important">
    <div id="accion-1-celda-0-0-0" class="elemento comentarios">1</div>
  </td>
  <td class="nota relative " style="text-align:center; color: #000000!important">
    <div class="elemento comentarios"><span id="accion-1-editar-1-0" class="block left ellipsis span  comentario" title=""></span><span id="accion-1-prismaticos-1-0" class="glyphicons glyph_observaciones observacion right"></span></div>
  </td>
  <td class="nota relative " style="text-align:center; color: #000000!important">
    <div id="accion-1-celda-2-0-0" class="elemento comentarios"></div>
  </td>
  <td class="nota relative " style="text-align:center; color: #000000!important">
    <div id="accion-1-celda-3-0-0" class="elemento comentarios"></div>
  </td>
</tr>

我们对元素感兴趣

&lt;div id="accion-1-celda-0-0-0" class="elemento comentarios"&gt;1&lt;/div&gt;

正在添加到IList&lt;IWebElement&gt;

但是,当尝试 SendKeys 到元素时,第一次它会正常工作,但是第二次总是会失败 StaleElementReferenceException,这是因为前一个元素(第一个)已经改变并且页面 DOM 也发生了变化。

如果StaleElementReferenceException 被抛出,我正在尝试找到再次找到该元素的方法。

到目前为止,这两种方法都失败了:

方法一

public virtual void Introducir_NotasAlumnos(string nota)
{
    IList<IWebElement> divNota = tablaNotas
    .Select((element, index) => element.FindElement(By.Id("accion-1-celda-0-" + index + "-0")))
    .ToList();

    divNota.ToList().ForEach(element => Introducir_Nota(element, nota));
}

方法二

public virtual void Introducir_NotasAlumnos(string nota)
{
    int index = 0;

        foreach (IWebElement element in tablaNotas)
        {
            By locator = By.Id("accion-1-celda-0-" + index + "-0");

            Introducir_Nota(element.FindElement(locator), nota);

            index++;
        }
}

感谢您的宝贵时间。

【问题讨论】:

  • 什么是tablaNotas
  • tablaNotas 定义如下: [FindsBySequence] [FindsBy(How = How.Id, Using = "tabla-virtual")] [FindsBy(How = How.CssSelector, Using = "div.row .row-overflow.bodyPlaceHolder")] [FindsBy(How = How.CssSelector, Using = "table.table")] [FindsBy(How = How.TagName, Using = "tbody")] [FindsBy(How = How. TagName, Using = "tr")] private IList tablaNotas { get;放; }
  • 您确定By.Id("accion-1-celda-0-" + index + "-0"); 选择器正确。在提供的html中有accion-1-celda-0-0-0accion-1-celda-2-0-0accion-1-celda-3-0-0元素,但accion-1-celda-0-1-0不存在。
  • 考虑到我只是粘贴了一个tr,但是每个tr中都复制了结构,所以对于tr0accion-1-celda-0-0-0存在,对于tr1accion-1-celda-0-1-0存在,等等
  • 检查我的答案,让我知道我的代码是否有效。我的 Java 代码但几乎没有区别

标签: c# html selenium exception staleelementreferenceexception


【解决方案1】:

这里是你的定位器:

  • 表格.table tbody &gt; tr
  • 按索引的表格行.table tbody &gt; tr:nth-child(1)

你的方法(java代码):

int size = driver.findElements(By.cssSelector(".table tbody > tr")).size();

for (int i = 0; i < size; i++) {
    WebElement row = driver.findElement(By.cssSelector(".table tbody > tr:nth-child(" + i + ")"));
    By locator = By.id("accion-1-celda-0-" + i + "-0");
    Introducir_Nota(row.findElement(locator), nota);
}

你有一定数量的行并且你独立找到行元素,不应该抛出StaleElementReferenceException异常。

这里是较短的版本:

int size = driver.findElements(By.cssSelector(".table tbody > tr")).size();

for (int i = 0; i < size; i++) {
    Introducir_Nota(row.findElement(By.cssSelector("#accion-1-celda-0-" + i + "-0")), nota);
}

【讨论】:

  • 感谢@Sers,我使用了您的解决方案并且工作正常。然而,令我失望的是,没有明确的方法可以“刷新”或获取 IWebElement,以便在发生类似情况时可以再次搜索该元素。谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2017-05-09
  • 2016-02-14
  • 1970-01-01
相关资源
最近更新 更多