在forEach循环中创建promise,并在不嵌套的情况下使用它们
Creating promises in a forEach loop and using them without nesting
我使用的是selenium-webdriver,我通过id获取特定元素。在获得具有正确id的元素后,我尝试从列表中选择一个具有唯一属性值的元素。
我已经设法达到了我需要的价值,但我对我找到的解决方案不满意。
情况:
var attributeName = "bla";
var id = "icon";
var iconElementsPromise = driver.findElements(By.id(id)); // returns a promise containing an array with WebElements
解决方案1:嵌套then
的:这看起来很错误,但它有效。
iconElementsPromise
.then(function(iconElements) {
iconElements.forEach(function(element, index) {
element.getAttribute(attributeName)
.then(function(attribute) {
console.log("attribute: " + attribute);
});
});
});
解决方案2:只有1个嵌套的then
:它看起来更好,但现在我需要创建一个Promises数组,这是可以的……但我希望根本不嵌套
iconElementsPromise
.then(function(iconElements) {
var promises = [];
iconElements.forEach(function(element, index) {
promises.push(element.getAttribute("tag"));
});
return promises;
})
.then(function(promises) {
Promise.all(promises)
.then(function(attributes) {
console.log("attributes: " + attributes);
});
});
解决方案3:通过返回Promise.all(promises)
,我可以保持在相同的indet级别,并且不嵌套then
s。
iconElementsPromise
.then(function(iconElements) {
var promises = [];
iconElements.forEach(function(element, index) {
promises.push(element.getAttribute(attributeName));
});
return promises;
})
.then(function(promises) {
return Promise.all(promises);
})
.then(function(attributes) {
console.log("attributes: " + attributes);
});
解决方案1有2个then
,并获得每个属性
解决方案2和3各有3个then
,并为我获取属性数组
获取每个属性或只获取数组是可以的。
我相信解决方案3或多或少是我想要的。但代码相当长。我觉得必须有一种更好、更可读、更短的方法来获取属性。
所以我的问题是:**使用promise获取属性的最佳方式是什么?**
示例值得赞赏。
3
的一个较短版本,使用map
减少一个then
,同时保持其可读性
iconElementsPromise
.then(function(iconElements) {
return Promise.all(iconElements.map(function(element){
return element.getAttribute(attributeName);
}));
})
.then(function(attributes) {
console.log("attributes: " + attributes);
});
如果您的目标是选择一个具有唯一属性的元素,那么将该属性包含在定位器中会更容易:
// select with an XPath the first element having an attribute "bla" and a parent with the "icon" id
var element = driver.findElement(By.xpath("id('icon')/*[@bla]"));
// select with a CSS selector the first element having the attribute "bla" and a parent with the "icon" id
var element = driver.findElement(By.cssSelector("#icon > [bla]"));
但是,如果你真的想要所有的属性,那么一个简单的解决方案是使用webdriver.promise.map
:
var webdriver = require('selenium-webdriver');
var promise = webdriver.promise;
var By = webdriver.By;
var Key = webdriver.Key;
var EC = webdriver.until;
var driver = new webdriver.Builder()
.withCapabilities({'browserName': 'firefox'})
.build();
driver.get('http://stackoverflow.com/');
driver.findElements(By.xpath('//a')).then(function(elts) {
promise.map(elts, function(elt) {
return elt.getAttribute("href");
}).then(function(links) {
console.log(links);
})
});
相关文章:
- 默认情况下折叠和展开嵌套列表
- Chrome:如何在不嵌套的情况下将两个连续跨距插入可编辑实体中
- 如何在不丢失格式的情况下连续淡入() 元素文本的每个字符,包括嵌套元素
- Angular UI Router-在不了解第二级嵌套视图的情况下更改第三级嵌套视图
- 在forEach循环中创建promise,并在不嵌套的情况下使用它们
- 在不知道客户端的情况下通过套接字捕获事件
- 如何在嵌套级别不受限制的情况下显示对象的动态嵌套子数组对象
- 如何在没有嵌套模板的情况下拥有嵌套路由
- 是否可以在不使用浏览器插件的情况下在标准 Web 应用程序中获取嵌套文件夹信息
- 木偶.js复合视图 - 如何在不嵌套的情况下渲染模型和集合
- 如何在不传递回调函数的情况下将嵌套对象属性作为选项传递
- 在不使用.get().set()的情况下更新Ember.js中嵌套对象中的值
- AngularJS$q中的两个嵌套线程是否可以在没有$q.dedefe()的情况下分叉和连接
- 如何在没有null引用异常的情况下轻松获得Javascript中嵌套字段的值
- 聚合物:在纸张项下嵌套纸张输入将禁用空格键
- 如何在不知道其位置的情况下删除Mongo对象(嵌套的2个数组)
- 如何在没有嵌套回调的情况下访问变量
- 我怎样才能在不让第一个循环循环到第二个循环的情况下嵌套for循环呢?
- 有没有一种方法可以在不使用嵌套循环的情况下将N个长度为N的数组's推入数组中?
- d3.js:访问向下嵌套的数据