To disable pop-ups and ads in Puppeteer, you can use the userAgent
and permissions
options in the launch
method of the Puppeteer browser. Here's an example code snippet to disable pop-ups and ads:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false, // Set to true if you want to run Puppeteer in headless mode
args: ['--no-sandbox', '--disable-setuid-sandbox'],
defaultViewport: null,
ignoreHTTPSErrors: true,
devtools: true,
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36', // Set a common user agent
permissions: ['geolocation', 'browserNotification'], // Set the permissions you want to grant or block
});
const page = await browser.newPage();
await page.goto('https://example.com');
// Your Puppeteer code here
await browser.close();
})();
In the above code snippet, we set a custom user agent using the userAgent
option and grant or block specific permissions using the permissions
option. You can customize the user agent and permissions based on your requirements to disable pop-ups and ads in Puppeteer.