【发布时间】: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>
每个<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++;
}
}
感谢您的宝贵时间。
【问题讨论】:
-
什么是
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-0、accion-1-celda-2-0-0和accion-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