[英]Puppeteer - how to return a result in page.evaluate within setTimeout
我正在使用 puppeteer 向底部滚动页面,当我按下一个键时,我想从 function 退出并返回结果。
该脚本几乎可以正常工作,除了我无法返回值和脚本结尾。
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false,
userDataDir: "C:\\Users\\johndoe\\AppData\\Local\\Google\\Chrome\\User Data\\Default"
});
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 1,
});
await page.goto('https://www.facebook.com/groups/0000000/members',{waitUntil: 'networkidle0'});
let rawMembers = await page.evaluate(() => {
const intervall = 3000;
let stop = false;
document.addEventListener('keypress', e => stop = true); //press a key to exit
let results = [];
let i = 0;
let pageHeigth = 0;
let timerId = setTimeout(function tick() {
if ((stop === false) && (document.body.scrollHeight > pageHeigth)){
pageHeigth = document.body.scrollHeight //save the current page heigth
document.scrollingElement.scrollTop = pageHeigth; //move the scroll to the end of the page (page visible size), this will couse new content to be loaded - virtula scroll)
results.concat(pageHeigth); //<--- it should be the results
timerId = setTimeout(tick, intervall); //schedule a new timeout to repeat the function
}
else
{
clearTimeout(timerId)
return results;
}
}, intervall);
});
console.log('END')
//await browser.close();
})();
您可以返回 Promise 并在计时器终止时解决它(另外,如其他答案中所述, concat()
在这里不适合,您可以改用push
):
const rawMembers = page.evaluate(() => new Promise((resolve) => {
const intervall = 3000;
let stop = false;
document.addEventListener('keypress', () => { stop = true; });
const results = [];
let pageHeigth = 0;
let timerId = setTimeout(function tick() {
if (stop === false && document.body.scrollHeight > pageHeigth) {
pageHeigth = document.body.scrollHeight;
document.scrollingElement.scrollTop = pageHeigth;
results.push(pageHeigth);
timerId = setTimeout(tick, intervall);
} else {
clearTimeout(timerId);
resolve(results);
}
}, intervall);
}));
concat
方法返回一个新数组。 它没有修改现有数组。 当您调用concat
方法时,您需要将其分配给一个变量。 所以这可能会解决问题
results.concat(pageHeight) //instead of this
let finalResult = results.concat(pageHeight) // you need to do this
其他方式你的results
数组总是空的,因为concat
方法不修改数组。 这是文档。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat#return_value
最后你需要返回finalResult
而不是results
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.