puppeteer newPage.evaluate 模拟页面滑动效果

4 min read

newPage.evaluate 模拟页面滑动效果

async function autoScroll(page){
    await page.evaluate(async () => {
        console.log(window.scrollBy())
        await new Promise((resolve, reject) => {
            var totalHeight = 0;
            var distance = 100;
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if(totalHeight >= scrollHeight){
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });

}