Javascript 如何返回量角器中嵌套承诺的元素
我在量角器中创建了以下函数:Javascript 如何返回量角器中嵌套承诺的元素,javascript,selenium,protractor,Javascript,Selenium,Protractor,我在量角器中创建了以下函数: function getChildElementByText(parentElement, tagName, textToSearch) { return parentElement.all(by.tagName(tagName)) .then((items) => { items.map( item => { item.getText().then(text => { if (tex
function getChildElementByText(parentElement, tagName, textToSearch)
{
return parentElement.all(by.tagName(tagName))
.then((items) => {
items.map( item => {
item.getText().then(text => {
if (text === textToSearch){
return item;
}
});
});
});
}
获取
元素的
元素的示例:
let myitem = selectorHelpers.getChildElementByText(clientIdSelect, 'option', 'ExampleText');
myitem.click();
这给了我以下错误:
Failed: myitem.click is not a function
我需要函数返回与的文本条件匹配的项,然后单击该元素,如上例所示。使用
map
获取单个元素可能不是最好的方法<代码>映射基于每个项的返回值返回一个新数组
考虑使用for…或
或甚至是经典的for
循环遍历每个项目,并使用异步/等待
等待每个项目。getText()
在继续下一个项目之前解析。然后,如果等待的结果等于textToSearch
返回项
对象。否则,在循环后返回null
function getChildElementByText(parentElement, tagName, textToSearch) {
return parentElement.all(by.tagName(tagName))
.then(async items => {
for (const item of items) {
const text = await item.getText();
if (text === textToSearch) {
return item;
}
}
return null;
});
}
您的第一个
。然后的回调不会返回任何内容。@jornsharpe第一个的返回应该在哪里。然后呢?抱歉,我是javascript承诺的初学者。在回调中只有一个表达式需要返回,items.map(…)
。但是对该映射的回调也不会返回任何内容。