DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Handle JavaScript Dialogs with Puppeteer

Use Puppeteer’s Page dialog event to inspect and respond to JavaScript alerts, confirms, prompts, and beforeunload dialogs.

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.

Listen for the page’s dialog event before triggering an alert, confirm, prompt, or beforeunload dialog. In the handler, call and await dialog.accept() to proceed or dialog.dismiss() to cancel. For a prompt, pass the text to enter to accept(promptText).

Handle a dialog with a Page event listener

Register the listener on the Puppeteer Page that will open the dialog, and do so before the action that triggers it. This example supplies text to prompts and dismisses other dialog types; change those choices to match the behavior your test is meant to verify.

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

  if (dialog.type() === 'prompt') {
    await dialog.accept('example response');
    return;
  }

  await dialog.dismiss();
});

The dialog event provides a Dialog instance. The example logs the dialog type and displayed message, then responds. Both response methods return promises, so await them.

Choose whether to accept or dismiss

Method Effect When to use it
dialog.accept() Accepts the dialog and lets the page continue. Use when the test should confirm or proceed.
dialog.accept(promptText) Accepts a prompt and supplies the given text. The argument has no effect on non-prompt dialogs. Use when the test should submit a prompt response.
dialog.dismiss() Dismisses the dialog. Use when the test should cancel, such as rejecting a confirmation.

There is no universal default response: accepting and dismissing produce different page outcomes. Select the one that exercises the scenario under test.

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

Inspect the dialog before responding

The Dialog API exposes information that can help a handler make a deliberate choice:

  • dialog.type() returns the type, such as alert, confirm, prompt, or beforeunload.
  • dialog.message() returns the displayed message.
  • dialog.defaultValue() returns the prompt’s default value, or an empty string for a non-prompt dialog.

For example, use type() to branch by dialog kind, or use defaultValue() if the test should submit the prompt’s displayed default rather than a fixed string. These inspection methods do not replace the need to accept or dismiss the dialog.

Set up Puppeteer and the browser

Install and import Puppeteer using the module style and package manager used by your project. The Puppeteer project distinguishes puppeteer, which downloads a compatible Chrome during installation, from puppeteer-core, which does not download Chrome. If your package manager blocks install scripts, you may need to install the browser explicitly. This setup is separate from handling dialogs.

Troubleshoot dialog handling

  • The page action appears stuck: make sure the listener is attached to the correct Page and is registered before the action that opens the dialog. Then ensure the handler awaits either accept() or dismiss().
  • The prompt contains the wrong text: pass the intended string to accept(promptText). If the test should use the displayed default, inspect defaultValue() first.
  • The test follows the wrong branch: inspect type() rather than assuming every JavaScript dialog is a prompt or confirmation. The Page event can cover alerts, prompts, confirms, and before-unload dialogs.
  • The page proceeds when it should cancel, or vice versa: revise the test’s response policy. Accept proceeds; dismiss cancels. Do not apply one response to every dialog unless that is actually the intended test behavior.

Or skip the browser setup

For a screenshot rather than an interactive Puppeteer test, ScreenshotNeo provides a one-request website screenshot API. Its capture flow accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and the response identifies the page verdict and billing status in headers. Its MCP server provides screenshot tools for AI agents.

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

Example cURL request (replace the URL with the page to capture):

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. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for free.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does the prompt text argument affect alerts or confirmations?

No. The string passed to accept(promptText) supplies input for a prompt; it has no effect on non-prompt dialogs.

What does defaultValue() return for a non-prompt dialog?

It returns an empty string.

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.

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. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver 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.