如何获取量角器中所有元素的文本 [英] How to get the text of all elements in protractor
问题描述
我要在Google搜索栏"Webdriver"中输入所有建议的li
文本.我已经写了一些这样的代码:
I want to fetch all the li
texts of suggestions after entering in the google search bar "Webdriver". I have wrote some code like this:
this.getElements = function(){
element.all(by.css('ul.sbsb_b')).then(function(text){
for(var i=0; i < text.length;i++)
{
console.log(text[i].getText());
}
});
};
执行时,我得到类似的东西:
On execution I m getting something like:
{ ptor_:
{ controlFlow: [Function],
schedule: [Function],
getSession: [Function],
getCapabilities: [Function],
quit: [Function],
代替建议"的文本值.
推荐答案
Protractor中有一组类似于javascript数组的函数,可让您过滤异步诺言的filter()或map()结果.例如,如果原始的"element.all(by.css('something'))"将返回ElementFinders的列表,则map()允许将其转换为getText()的数组,处于解析状态的该数组只是一个字符串数组.然后,您可以根据需要使用它.
There is a set of javascript array-like functions in Protractor that allows you to filter() or map() result of async promise. For example if original "element.all(by.css('something'))" would return list of ElementFinders, map() allows to transform it into array of getText()s, which in resolved state is just an array of strings. You can then use it however you want.
element.all(by.css('something')).map(function(elm) {
return elm.getText();
}).then(function(texts) {
texts.forEach(...);
});
这篇关于如何获取量角器中所有元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!