To take a screenshot after each page interaction in Puppeteer, you can use the following code snippet:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate to the desired page
await page.goto('https://www.example.com');
// Perform interactions on the page
// For example, click a button
await page.click('button');
// Take a screenshot after the interaction
await page.screenshot({ path: 'screenshot1.png' });
// Another interaction example - scroll down the page
await page.evaluate(() => {
window.scrollBy(0, window.innerHeight);
});
// Take another screenshot after the interaction
await page.screenshot({ path: 'screenshot2.png' });
// Close the browser
await browser.close();
})();
In this code snippet, we first launch a new Puppeteer browser instance and create a new page. We then navigate to a specific URL and perform interactions on the page, such as clicking a button or scrolling down.
After each interaction, we use the page.screenshot()
method to capture a screenshot of the page and save it to a file. You can customize the path and file name of the screenshots as needed.
Remember to adjust the interactions and URLs based on your specific requirements.