To disable images on a page to improve Puppeteer performance, you can use the following code snippet:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
if (request.resourceType() === 'image') {
request.abort();
} else {
request.continue();
}
});
await page.goto('https://example.com');
// Your Puppeteer code to interact with the page goes here
await browser.close();
})();
In this code snippet, we enable request interception on the page and then listen for requests. If a request is for an image (request.resourceType() === 'image'
), we abort that request. This prevents images from being loaded on the page, which can improve Puppeteer performance.
You can include this code snippet in your Puppeteer script to disable images on specific pages where you don't need them, thus improving the performance of your Puppeteer automation.