我有一个用户列表,按表显示,我需要搜索具有适当值的td元素 - 如果当前页面上不存在元素 - 单击链接转到另一页面。 我不知道如何做到这一点,但我使用xpath选择器来查找具有一定价值的元素。
每个用户都由“tr”元素表示,并在其中包含“td”元素:
<tr>
<td class="break-all">acceptance</td>
<td>client</td>
<td class="break-all"></td>
</tr>
所以,我需要找到值=“接受”的td元素,如果它在当前页面上不存在 - 点击链接转到另一页并继续搜索。
我找到了解决方案,它对我有用!
function isActive(client) {
client.getAttribute("div.right", "class", function (result) {
if (result.value == "right active") {
client
.click("div.icon-caret-right")
.perform(function () {
findByText(client)
})
} else {
//Throw error in this case!!!
}
})
}
function findByText(username, client) {
client.elements("xpath", "//td[text()='" + username + "']", function (result) {
if (!result.value.length) {
isActive(client)
}
})
}
module.exports = findByText;
我只是致电findByText(username, client)
。
但现在我还有另一个问题 - 如何从else
语句???
答案 0 :(得分:1)
也许这样的事情,可能会有一个更简单的解决方案:
function findStringInElementsData(elements, stringToFind, callback)
{
var counter = 0,
numberOfElements = elements.value.length;
elements.value.forEach(function(element){
browser.elementIdText(element.ELEMENT, function(result){
// The string was found, return true
if(result.value === stringToFind) {
return callback(true);
}
// The string was not found return false
if(numberOfElements-1 === counter) {
return callback(false);
}
counter++;
})
})
}
browser
.waitForElementVisible('td', 2000)
.elements('css selector', 'td', function(elements){
findStringInElementsData(elements, "acceptance", function(foundString) {
if(foundString) {
// The string was found
} else {
// The string was not found, click on link to go on another page
}
})
})
上面的代码将获取所有td
元素,然后调用findStringInElementsData
函数,它将遍历它们以查看它们的文本值等于提供的字符串(&#34;接受&#34 ;在你的情况下)。如果找到该字符串,该函数将返回true,否则它将返回false。