Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
FREEDOM251

Freedom251 › Blog › browser automation

How to Cancel a File Chooser Dialog in Puppeteer

By Freedom251 Team · · 2 min read

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

To cancel a file chooser intercepted by Puppeteer, register page.waitForFileChooser() before triggering the chooser, then call await fileChooser.cancel(). This closes the chooser without selecting a file.

Cancel a file chooser

Use Promise.all() to start waiting before clicking the control that opens the chooser:

As an Amazon Associate I earn from qualifying purchases.

const [fileChooser] = await Promise.all([
  page.waitForFileChooser(),
  page.click('#upload-file-button'),
]);

await fileChooser.cancel();

Replace #upload-file-button with a selector for the page’s upload control. The returned object is a Puppeteer FileChooser; its cancel() method closes the chooser without selecting files. Puppeteer FileChooser reference · cancel() method

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

Why the waiter must come first

waitForFileChooser() listens for a chooser opened after the waiter is registered; it does not attach to a chooser that is already active. Starting the wait and the click together with Promise.all() avoids a race in which the click opens the chooser before Puppeteer is listening. Page.waitForFileChooser() reference

Canceling versus selecting a file

Call cancel() when the automation should close the chooser without uploading anything. If the test is meant to provide files instead, use fileChooser.accept(paths), where paths is an array of file paths. Every intercepted chooser should be accepted or canceled; leaving one unresolved can prevent later chooser requests from appearing. Puppeteer FileChooser reference

What this method does not handle

  • DOM file picker APIs: Puppeteer documents that waitForFileChooser() does not currently intercept window.showOpenFilePicker(). Page.waitForFileChooser() reference
  • Visible native picker UI: When Puppeteer handles the chooser with this method, the native picker does not appear to the user, including in headful mode. Page.waitForFileChooser() reference

Do not use JavaScript dialog dismissal

A browser file chooser and a JavaScript alert, confirm, or prompt are different Puppeteer objects. Dialog.dismiss() applies to JavaScript dialogs; use FileChooser.cancel() for the file chooser. Puppeteer Dialog reference

Troubleshooting

  • The wait times out: Check that the selector points to the control that actually opens a file chooser, and register waitForFileChooser() before clicking it. The waiter will not capture an already-open chooser.
  • A later chooser never appears: Make sure each earlier intercepted chooser was resolved by calling accept() or cancel().
  • window.showOpenFilePicker() is involved: This API is not intercepted by waitForFileChooser(); Puppeteer’s documented waiter is not a solution for that picker path.
  • An old example omits await: Current Puppeteer documents cancel(): Promise<void>, so await it. The changelog records a historical change making cancellation synchronous in Puppeteer 9.0.0, released April 21, 2021; check the installed version when maintaining older automation. Current cancel() reference · Puppeteer changelog
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 clean screenshot rather than automating a file upload, ScreenshotNeo provides a one-request screenshot API. It accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify page verdict and billing status. Its MCP server includes screenshot tools for AI agents.

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.

cURL example; replace YOUR_API_KEY with your key:

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

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.

browser automationfile chooserJavaScriptPuppeteer

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.

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.