假设我有以下 DOM 结构
<table id="campaigns">
<tr>
<th>Name</th>
<th>Status</th>
</tr>
<tr>
<td>first data</td>
</tr>
<tr data-id="1">
<td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
</tr>
<tr data-id="2">
<td>intern test at Fri, 09 Dec 2016 03:12:26 GMT</td>
</tr>
<tr data-id="3">
<td>intern test at Fri, 09 Dec 2016 03:12:26 GMT edit</td>
</tr>
<tr data-id="4">
<td>another data</td>
</tr>
<tr data-id="5">
<td>next data</td>
</tr>
</table>
这就是我在实习生中所做的
this.remote
.findAllByXpath("//*[@id='campaigns']/tr"") // get allitem from list
.then(function (options) {
return Promise.all(options.map(function (option) {
return option.getVisibleText()
.then(function (text) {
if (text.includes("intern test")) { // checked whether the value is exist
option.click(); // click the value
}
return text;
})
}))
})
我的要求只是点击一个包含“实习生测试”的文本。 这段代码看起来不错,但发生的情况是,当实习生已经单击 if 语句中的第一个元素时,下一个元素似乎也会执行单击并陈旧该元素。 当找到第一个元素时,如何停止此循环并继续?
感谢您提供的任何帮助
最佳答案
无需遍历所有元素来查找包含文本“intern test”的第一个元素
使用 findAllByXpath
尝试以下 XPath(我不知道 Javascript - selenium,因此,如果需要,请更正语法):
//table[@id='campaigns']/tbody/tr/td[contains(text(),'intern test')]/..
返回 ID 为 campaigns
的表中文本包含 intern test
的所有 tr
元素。
使用索引,例如 0, 1, 2
来匹配包含 intern test
的第一个、第二个和第三个
元素
关于javascript - 如何打破实习生中的元素循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41055505/