在处理 Selenium 中的动态 table 时避免 StaleElementReferenceException

Avoiding StaleElementReferenceException when dealing with a dynamic table in Selenium

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

这是我们的 table:

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

每个 <tr> 包含以下内容:

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

我们对元素感兴趣

<div id="accion-1-celda-0-0-0" class="elemento comentarios">1</div>

正在添加到 IList<IWebElement>

然而,当试图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++;
        }
}

感谢您的宝贵时间。

这是您的定位器:

  • table: .table tbody > tr
  • table 行按索引.table tbody > 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);
}