Converting an array of promises from Selenium's findElements into an array of objects

Deadly 提交于 2019-12-06 05:15:14

There's no need to aggregate the promises with all since findElements returns a single promise. You also need to iterate the resolved array with a for loop and not a for-in loop:

driver.findElements(webdriver.By.xpath(my_xpath)).then(function(elements) {
    for (var i = 0; i < elements.length; i++) {
        elements[i].getText().then(function(text) {
            console.log(text);
        })
    };
});

You could also use map to directly get an array with the text for each element :

var map = webdriver.promise.map;

var elements = driver.findElements(By.xpath(my_xpath));

map(elements, e => e.getText()).then(function(texts) {
  console.log(texts);
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!