我想检索在第三列中过滤出的所有行。我已经附上了网站参考结构的图片,这是实际的网站http://juliemr.github.io/protractor-demo/
//I found this online, but its not working for me it still retrieve all the data from the table
tablefil(){
//third column id like to filter
var name = "11";
// This is like element.all(by.css(''))
return $$("table[class='table']").filter(function(row) {
// Get the second column's text.
return row.$$("td[class='ng-binding']").get(3).getText().then(function(rowName) {
// Filter rows matching the name you are looking for.
return rowName === name;
});
}).getText().then(function(text){
//Display rows that fits my filter above
console.log('\n'+text+'\n')
})
}
[1]: https://i.stack.imgur.com/z8Heb.png
答案 0 :(得分:2)
您的代码中有两个问题:
1)不正确的CSS选择器无法找到表的所有行。正确的是:table[class='table'] tbody > tr
2)数组索引从“ 0”开始,因此获取第三列应使用
row.$$("td").get(2)
tablefil(result){
return $$("table[class='table'] tbody > tr")
.filter(function(row) {
// find out which row's Result is equal to argument: result
return row.$$("td").get(2).getText().then(function(text) {
return !result || text.trim() === result;
});
})
.each(function(row){
// read the text of 2nd column: Expression on matched row
row.$$("td").get(1).getText().then(function(text){
console.log('expression: ' + text)
});
// or if you want to print the Expression and Result together
row.$$("td:nth-child(2),td:nth-child(3)").getText().then(function(texts){
console.log(texts[0], '=', text[1])
});
})
}
tablefil() // will print all rows
tablefil('11') // will only print row's Result == 11