[英]Cannot access in the second page.evaluate a variable defined in the first page.evaluate (node.js puppeteer)
[英]Node.js Puppeteer How To Await For page.evaluate() Method
我需要等待滾動操作完成,然后解決。
當我將 resolve() 放入 page.evaluate() 塊時,它會拋出:(node:22646) UnhandledPromiseRejectionWarning: Error: 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();
});
我刪除了 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.