簡體   English   中英

Node.js Puppeteer 如何等待 page.evaluate() 方法

[英]Node.js Puppeteer How To Await For page.evaluate() Method

我需要等待滾動操作完成,然后解決。

當我將 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

我刪除了 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);
        });
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM