How can I use Puppeteer to scroll to the bottom of a page until I can’t scroll anymore?
Hope this message finds you well! I’m thrilled to share a dazzling snippet of code that will gracefully transport you to the bottom of the page using a combination of window.scrollTo and page.evaluate. Picture this as a magical journey orchestrated with precision and finesse.
Behold the enchanting script:
Scroll to the bottom using window.scrollTo and page.evaluate: s method uses window.scrollTo to scroll to the bottom of the page and page.evaluate to execute the scrolling in the page context.
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});