M
M
Meyz2018-05-20 10:09:49
JavaScript
Meyz, 2018-05-20 10:09:49

Why doesn't reduce work in node.js/selenium?

There is such a code, it should go to the VK news page and parse the news, but for some reason it displays only one news. What am I doing wrong?

await driver.get('https://vk.com/feed');
        await driver.sleep(500);
        const result = await driver.findElements(By.className('_post'));
      const arr = await result.reduce(async (res, item, index) => {
            try {
                const textItem = await item.findElement(By.className('wall_post_text'));
                if (textItem) {
                    const text = await textItem.getText();
                    console.log(text) // если тут ставлю лог, то видно, что новость на самом деле не одна
                    res.push(text);
                    console.log(123) // а этот лог срабатывает только один раз, получается пуш почему-то игнорируется...
                }
            }
            catch(e) {
                if(e.name === 'NoSuchElementError') {
                    console.log('Element not found');
                }
            }
            finally {
                return res;
            }
        }, []);
console.log(arr); // возвращает только одну новость

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question