Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
FREEDOM251

Freedom251 › Blog › browser automation

How to Get the Default Value of a Puppeteer Dialog

By Freedom251 Team · · 2 min read

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

Listen for the page’s dialog event, then read dialog.defaultValue(). For a prompt, it returns the text initially shown in the prompt; for other dialog types, it returns an empty string.

Read a prompt’s default value

Puppeteer emits a dialog event on the page when a JavaScript dialog opens. The event handler receives a Dialog object, whose defaultValue() method reads the prompt’s initial text.

As an Amazon Associate I earn from qualifying purchases.

The following handler logs that value and accepts the dialog:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
page.on('dialog', async dialog => {
  const initialText = dialog.defaultValue();
  console.log(initialText);
  await dialog.accept();
});

dialog.defaultValue() returns an empty string if the dialog is not a prompt. Puppeteer documents the supported dialog types as alert, beforeunload, confirm, and prompt. See the Puppeteer Dialog API reference.

Accept the prompt with different text

To submit a replacement instead of accepting the original prompt value, pass a string to accept():

page.on('dialog', async dialog => {
  const initialText = dialog.defaultValue();
  console.log('Initial prompt value:', initialText);
  await dialog.accept('replacement text');
});

The optional argument supplies the prompt’s response; it has no effect on other dialog types. The browser’s prompt(message, default) function uses its second argument as the initial response text. See the HTML Living Standard.

Handle dialog types deliberately

If your handler may receive dialogs other than prompts, check the type before relying on the value. The API returns an empty string for a non-prompt dialog, so the value alone cannot establish that the dialog is a prompt.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
page.on('dialog', async dialog => {
  if (dialog.type() === 'prompt') {
    console.log('Initial prompt value:', dialog.defaultValue());
    await dialog.accept();
    return;
  }

  // Choose the appropriate action for alert, confirm, or beforeunload.
  await dialog.dismiss();
});

Choose the non-prompt action to suit your test: accepting a confirmation and dismissing it represent different user responses. This example dismisses non-prompt dialogs only to show an explicit branch; it is not a universal handling policy.

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 to capture a website rather than inspect a JavaScript prompt, ScreenshotNeo provides a screenshot API. One GET request can return a PNG, JPEG, WebP, or PDF. For example, this cURL call saves a WebP screenshot:

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 for request options. ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, failed loads, timeouts, and cache hits are not billed. Its MCP server lets AI agents take screenshots. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo and start with 1,000 free screenshots a month, no card required.

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.

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

browser automationJavaScriptPuppeteerWeb development

Leave a Reply

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

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

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.