我想用节点和webdriverJS遍历表硒:硒遍历表/ webdriverjs
<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
});
}
});
这适用于第一一些行,但有许多排它在某一点失败。
我的理论是:for循环中的findElement
调用被传递给管理器,然后for循环完成。然后垃圾收集器删除行数组。一旦经理执行findElement
调用,数组及其元素不再存在并失败。我得到的错误是:
StaleElementReferenceException : The Element is not Attached to the DOM
它的工作第一行作为数组仍然存在于早期的执行上。
我的问题:
我究竟做错了什么?
我的理论是否正确?
如何将
row[i]
引用绑定到findElement调用,以使它们保持比原始数组更长的时间?
---- ----编辑
当我删除内findElement电话中的一个,只追求每行一个单元,我能够覆盖更多的行。这让我觉得,在这个实施中,时间起着一定的作用。这不应该是这样,所以我可能做错了什么。
- Selenium中是否有像
forEach
函数?
如果您没有验证行本身的任何内容,您可以为每种单元类型编写一个XPath,并逐个对这些单元进行迭代以查看是否有效。基本上用'driver.findElements(By.xpath('// tr/td [@class = \'name \']'))'''''''''''''''driver'替换'driver.findElements(By.tagName('tr')) .findElements(By.xpath('// tr/td [@class = \'count'''))'。 – shri046 2014-11-05 18:01:39
我也在考虑这个选项,但我想保持一行的单元格。最后我想把名字和数字联系起来。这就是为什么我想要逐行浏览它,然后通过一行的单元格。 – pat 2014-11-05 22:07:26
如果你用'console.log(name)'和'console.log(count)'替换你的'//做一些东西''你还会**得到间歇性故障吗? – Louis 2014-11-05 23:56:07