Unlike other automation tools, puppeteer works a bit differently at Javascript alerts. Every automation tools take the decision whether to accept the alert or to dismiss after the alerts are thrown on UI. The user should provide the decision to the Puppeteer before it can handle the alerts.
Users must register events using the on event listener in the puppeteer so that the puppeteer can handle the alert.
In simple words, you should tell the puppeteer what it should if an alert occurs.
const puppeteer = require('puppeteer');
async function run(){
const browser = await puppeteer.launch({headless:false})
const page = await browser.newPage();
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.dismiss();
});
await page.goto('https://chercher.tech/practice/popups');
await page.click("input[name='alert']")
await browser.close();
}
run()
We can perform few operations on the alert, below are few operations that we can perform
page.on('dialog', async dialog => {
console.log(dialog.accept());
});
await page.goto('https://chercher.tech/practice/popups');
await page.click("input[name='alert']")
page.on('dialog', async dialog => {
await dialog.dismiss();
});
await page.goto('https://chercher.tech/practice/popups');
await page.click("input[name='confirm']")
page.on('dialog', async dialog => {
console.log(dialog.message());
});
await page.goto('https://chercher.tech/practice/popups');
await page.click("input[name='confirm']")
page.on('dialog', async dialog => {
console.log(dialog.defaultValue("1234));
});
await page.goto('https://chercher.tech/practice/popups');
await page.click("input[name='prompt']")
Alerts on the webpage used to get the attention of the user to perform some operation on the alert or on the webpage, sometimes alerts expect input from the user. Alert prevents the user from accessing other parts of the web page until the box is closed.
alert() method displays an alert box with a message and an OK button, an alert box is often used to inform a user about a particular operation like details saved in Database, right-click disabled, Loaded successfully such kind of messages.
confirm() method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to the developer.

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.