October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Get the Message from a Puppeteer Dialog

Read a Puppeteer dialog’s displayed text with dialog.message() inside the page’s dialog event handler, then accept or dismiss it.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Listen for the page’s dialog event, then call dialog.message() on the Dialog object passed to the handler. It returns the text shown in the browser dialog as a string.

Read the message in the dialog handler

Install the event listener before the page action that opens the dialog. Read its message, then accept or dismiss the dialog so it does not leave the page interaction waiting.

As an Amazon Associate I earn from qualifying purchases.

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

message() provides the displayed message; it does not read text from the page DOM. Puppeteer delivers the dialog through the Page’s dialog event. See the Dialog.message() API and the Dialog class example.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Complete runnable example

This example creates a browser and page, registers the listener, then triggers an alert. It logs the alert text and dismisses the dialog.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  try {
    const page = await browser.newPage();

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

    await page.evaluate(() => alert('The dialog message'));
  } finally {
    await browser.close();
  }
})();

Run it in a Node.js project with Puppeteer installed. The event handler must be set up before page.evaluate() triggers the alert, or the code may wait on a dialog that has no handler.

Message text versus a prompt’s default value

Use dialog.message() for the text displayed in the dialog. For a prompt’s pre-filled input, use dialog.defaultValue() instead. That method returns an empty string when the dialog is not a prompt. Puppeteer documents the dialog types as alert, beforeunload, confirm, and prompt; the message method applies to the Dialog API rather than only alerts.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Choose whether to accept or dismiss

After inspecting the message, call await dialog.accept() to accept the dialog, or await dialog.dismiss() to cancel it. Choose based on the behavior your test or automation needs. The official example dismisses after reading.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting

  • The script appears stuck: A JavaScript dialog may be blocking page progress. Register the handler before triggering the page action and ensure it accepts or dismisses the dialog.
  • The message is undefined or unavailable: Read it from the Dialog object supplied to the dialog event handler, not from the page object or DOM.
  • You need the prompt’s pre-filled text: Call dialog.defaultValue(); message() returns the displayed dialog message, not the prompt input default.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your goal is a screenshot rather than interacting with a JavaScript dialog, ScreenshotNeo offers a one-request screenshot API. For example, this cURL request captures a page:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation. Cookie banners, popups and chat widgets are removed before capture; bot checks, blank pages and failed loads are not billed. An MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots per month with no card, and paid plans start at $5 for 3,000.

Sign up free for ScreenshotNeo.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.