Node.js Puppeteer如何等待page.evaluate()方法 [英] Node.js Puppeteer How To Await For page.evaluate() Method

查看:532
本文介绍了Node.js Puppeteer如何等待page.evaluate()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to wait for scrolling action to finish, then resolve.

当我将resolve()放在page.evaluate()块中时,它抛出:
(节点: 22646)UnhandledPromiseRejectionWarning:错误:评估失败:ReferenceError:未定义解析

When I put resolve() inside page.evaluate() block it throws: (node:22646) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: resolve is not defined

如果我这样保存,它将立即解决。

If I keep it like this It resolves immediately.

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

推荐答案

我删除了for循环,不知道它的作用

I removed the for loop, don't know what it is there 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);
        });
    });
}

这篇关于Node.js Puppeteer如何等待page.evaluate()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆