How to handle pop-up dialogs in Puppeteer?

To handle pop-up dialogs in Puppeteer, you can use the page.on('dialog') method to listen for dialog events and interact with them as needed. Here's a basic example of how you can handle a simple alert dialog:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); page.on('dialog', async dialog => { console.log(dialog.message()); await dialog.dismiss(); // Dismiss the dialog // Other possible actions: dialog.accept(), dialog.dismiss(), dialog.prompt(), etc. }); await page.goto('your_url_here'); // Trigger the alert dialog await page.evaluate(() => { alert('Hello, world!'); }); await browser.close(); })();

In this example, we first listen for the dialog event on the page object. When a dialog is triggered, we log the dialog message and dismiss it using dialog.dismiss(). You can also interact with the dialog using other methods such as accept() or prompt(), depending on the type of dialog.

You can modify this example to handle different types of dialogs, such as confirm or prompt dialogs, by checking the dialog type and responding accordingly.