我想使用 Node 和 webdriverJS 使用 Selenium 遍历表:
<table>
<tr>
<td class="name">Peter</td>
<td class="count">1</td>
</tr>
<tr>
<td class="name">John</td>
<td class="count">3</td>
</tr>
</table>
我希望每一行都查看名称和行单元格。
我有的:
driver.findElements(By.tagName('tr')).then(function(rows){
// for every row
for (var i = 0; i< rows.length; i++){
// check the name cell
rows[i].findElement(By.class('name')).getInnerHtml().then(function(name){
// do some stuff
});
// check the count cell
rows[i].findElement(By.class('count')).getInnerHtml().then(function(count){
// do some stuff
});
}
});
这适用于前几行,但对于许多行,它会在某个点失败。
我的理论:findElement
for循环中的调用被传递给管理器,然后for循环结束。然后垃圾收集器删除 rows 数组。一旦管理器执行findElement
调用,数组及其元素就不再存在并失败。我得到的错误是:
StaleElementReferenceException : The Element is not Attached to the DOM
它确实适用于第一行,因为数组在执行的早期仍然存在。
我的问题:
我究竟做错了什么?
我的理论正确吗?
如何将
row[i]
引用绑定到 findElement 调用以使它们比原始数组持续更长的时间?
- - 编辑 - -
当我删除一个内部 findElement 调用并且每行只查找一个单元格时,我能够覆盖更多行。这让我认为,通过这种实施,时间发挥了作用。这不应该是这样,所以我可能做错了什么。
forEach
Selenium 中是否有类似函数的功能?