How to click button of prompt of webpage in javascript?

Viewed 602

So I was building this web automation with puppeteer in node js. This automation should go to zoom enter the meeting id and click join button.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto('https://us04web.zoom.us/join');
  await page.screenshot({path: 'example.png'});
  //await browser.close();
  
  await page.type("#join-confno", "123 456 7890", {delay: 100})
  await page.click("#btnSubmit")
})();

the script runs and after it runs, the browser opens and does the automation but this popped up that ruined my whole idea: this prompt

and now i want the script to press "open zoom meeting" im new to node js tho. Is there any idea how to do that?

1 Answers

Puppeteer dispatches Dialog objects via the 'dialog' event, when a JavaScript dialog appears (such as alert, prompt, confirm or beforeunload).

You can accept a dialog, like this:

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

You could read dialog.message(), which returns the message displayed in the dialog to confirm you're accepting the right dialog.

For example:

page.on('display', async (dialog) => {
  if (dialog.message().endsWith('wants to open this application.')) {
    await dialog.accept();
  }
});

Disclaimer: I haven't tested this code, but something like this should be possible.


Edit: Replying to your comment, you have to listen for 'dialog' events earlier.

Your example would look like this:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();

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

  await page.goto('https://us04web.zoom.us/join');
  await page.screenshot({ path: 'example.png' });
  
  await page.type('#join-confno', '123 456 7890', { delay: 100 });
  await page.click('#btnSubmit');
})();

I tested it with a basic page and it works as expected with a confirm dialog.

Related