【问题标题】:Node.js Puppeteer How To Await For page.evaluate() MethodNode.js Puppeteer 如何等待 page.evaluate() 方法
【发布时间】:2021-02-05 05:15:52
【问题描述】:

我需要等待滚动操作完成,然后解决。

当我将 resolve() 放入 page.evaluate() 块时,它会抛出: (node:22646) UnhandledPromiseRejectionWarning: E​​rror: Evaluation failed: ReferenceError: resolve is not defined

如果我保持这样它会立即解决。

let scrollingPromise = new Promise((resolve, reject) => {
    page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');            
        for (let i = 0; i < 4; i++) {
            setTimeout(function () {
                scrollingWindow.scrollBy(0, 3000);
            }, 2000 * i);
        }
    });
    resolve();
});

已解决 https://github.com/puppeteer/puppeteer/issues/844

【问题讨论】:

  • pptr.dev/… — 项目符号列表后面的第一段可能会有用。

标签: javascript node.js asynchronous puppeteer es6-promise


【解决方案1】:

我去掉了for循环,不知道它是干什么用的

function scroll(){
    return page.evaluate(() => {
        const scrollingWindow = document.querySelector('.section-layout.section-scrollbox.scrollable-y.scrollable-show');
        return new Promise(resolve=>{
            setTimeout(function () {
                scrollingWindow.scrollBy(0, 3000);
                resolve();
            }, 2000);
        });
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2019-09-03
    • 2020-03-19
    • 1970-01-01
    • 2019-04-05
    • 2018-02-22
    • 2020-05-09
    相关资源
    最近更新 更多