You can scroll to the bottom of a page in Puppeteer by using the page.evaluate()
method to execute JavaScript code within the browser context that will scroll to the bottom of the page. Here is an example code snippet:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.evaluate(() => {
window.scrollTo(0, document.body.scrollHeight);
});
// Optional: You can also use the below code to wait for a few seconds after scrolling to the bottom
await page.waitForTimeout(3000);
await browser.close();
})();
In this code snippet, we first launch a new Puppeteer instance, navigate to a webpage (https://example.com
in this case), and then use page.evaluate()
to execute JavaScript that scrolls to the bottom of the page by setting the scroll position to the height of the document.
You can also optionally use page.waitForTimeout()
to wait for a specific amount of time (in milliseconds) after scrolling to the bottom before closing the browser.
Remember to replace https://example.com
with the URL of the webpage you want to scroll to the bottom of.