October 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 ScanOctober 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 Accept a JavaScript Dialog with Puppeteer

Listen for Puppeteer’s dialog event and call accept(), pass text for prompts, or dismiss when the intended action is cancellation.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To accept a JavaScript alert, confirm, prompt, or beforeunload dialog with Puppeteer, listen for the page’s dialog event and call await dialog.accept(). Register the listener before the click or navigation that triggers the dialog. For a prompt, pass the text to enter to accept().

Accept a dialog with Puppeteer

Attach a handler to the page before triggering the action that opens the dialog:

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

The page emits a dialog event with a Dialog object. Its accept() method returns a promise that resolves when the dialog has been accepted. This event-and-handler approach is for JavaScript dialogs; it is not the same as finding and clicking a button in the page DOM. See the Puppeteer Dialog API.

Enter text in a JavaScript prompt

Pass the text as the optional argument to accept() when 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 => {
  await dialog.accept('text to enter');
});

The optional promptText argument only affects prompt dialogs. It has no effect on alerts, confirms, or beforeunload dialogs. For prompts, dialog.defaultValue() can return the prompt’s default value; it is empty for non-prompt dialogs. Refer to the accept() method reference and defaultValue() reference.

Dismiss instead of accepting

To cancel a dialog, call await dialog.dismiss() in the handler:

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
page.on('dialog', async dialog => {
  await dialog.dismiss();
});

dismiss() returns a promise that resolves when the dialog has been dismissed. Choose between accepting and dismissing according to the interaction your test is meant to perform. The method is documented in the Puppeteer dismiss() reference.

Inspect the dialog before deciding

Puppeteer sends several dialog types through the same event: alert, prompt, confirm, and beforeunload. If the page can open more than one kind, inspect the dialog before resolving it rather than automatically accepting every dialog.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • dialog.type() identifies the dialog type.
  • dialog.message() returns its message.
  • dialog.defaultValue() provides a default value for prompts and an empty value for other dialog types.
  • dialog.handled indicates whether the dialog has been handled.

These properties and dialog categories are described in the Dialog class reference and the PageEvent reference. The references surfaced for this topic span Puppeteer versions 25.4.0, 25.9.0, 25.11.0, and 25.12.0; the PageEvent page uses the next documentation path. If your installed package behaves differently, consult the API reference matching that package version.

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 you need a website screenshot rather than a browser dialog interaction, ScreenshotNeo offers a screenshot API and MCP server. A single GET request returns an image or PDF; for example, this cURL request saves a WebP screenshot of Stripe:

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 for request options. ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for 1,000 free screenshots a month, with no card required.

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
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.