如何使用 puppeteer 确认警报弹出窗口

How to confirm alert popup with puppeteer

我正在单击包含确认对话框但无法关闭的 link。

我尝试按 "Enter" 并使用 puppeteer 的方法关闭并接受对话框,但没有任何反应。

link:

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

我试过了:

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

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

确保在单击 link 之前 开始收听 dialog 事件。像这样:

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

await page.click('a');