Search code examples
javascriptnode.jspuppeteerwebautomation

How to confirm alert popup with puppeteer


I'm clicking on a link that contains a confirm dialog but not being able to dismiss.

I tried to press "Enter" and use puppeteer's method to dismiss & accept the dialog but nothing happened.

The link:

<a onclick="return confirm('Yes?');" id="link" href="google.com">

I tried:

page.on('dialog', async dialog => {
    console.log('here'); //does not pass
    await dialog.accept();
    //await dialog.dismiss();
});

and

await page.keyboard.press('Enter');
await page.keyboard.press(String.fromCharCode(13));

Solution

  • Make sure you start listening to the dialog event before clicking the link. Something like this:

    page.on('dialog', async dialog => {
      console.log('here');
      await dialog.accept();
    });
    
    await page.click('a');