ziad.ali
ziad.ali

Reputation: 341

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));

Upvotes: 8

Views: 8986

Answers (1)

hardkoded
hardkoded

Reputation: 21597

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');

Upvotes: 17

Related Questions