To scroll down a page using Puppeteer, you can use the page.evaluate
function to execute JavaScript code within the context of the page. Here's an example of how you can scroll down a page:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Scroll down the page by 1000 pixels
await page.evaluate(() => {
window.scrollBy(0, 1000);
});
await browser.close();
})();
In this example, we use the page.evaluate
function to execute a JavaScript function that scrolls down the page by 1000 pixels using window.scrollBy
. You can adjust the number of pixels to scroll by changing the value passed to scrollBy
.
Alternatively, you can also use the page.evaluate
function to scroll to a specific element on the page. Here's an example:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Scroll to a specific element on the page
const element = await page.$('#myElementId');
await page.evaluate((element) => {
element.scrollIntoView();
}, element);
await browser.close();
})();
In this example, we use the page.evaluate
function to scroll to a specific element on the page with the ID myElementId
using the scrollIntoView
method.