What are Chrome Dialogues?
A Chrome dialog is any pop-up or modal window that Google Chrome itself creates (not part of a web page) to communicate something to the user or request input. These include:alert()
dialogs that display a message with an “OK” buttonconfirm()
dialogs that ask for confirmation with “OK” and “Cancel” buttonsprompt()
dialogs that request text input from the userwindow.print()
dialogs that open the browser’s print interface
Why You Can’t Interact with Them Directly
Chrome dialogs block JavaScript execution on the page until they’re dismissed. This means:- Automation tools cannot interact with the page while a dialog is open
- Automation scripts will hang until the dialog is manually dismissed
Preventing Chrome Dialogues
The best approach is to prevent these dialogs from appearing in the first place by overriding the native dialog functions before any page code runs. This is done usingaddInitScript()
, which executes code before any page scripts.
Implementation
- Node.js
- Python
Playwright
Customizing Dialog Behavior
You can customize the return values based on your automation needs:Handling PDF Dialogues
PDF dialogues present unique challenges because PDFs can be generated and displayed in different ways. There are two common scenarios you’ll encounter:Scenario 1: PDF Opens in the Current Tab
When a PDF replaces the current page content, you need to capture it differently:- Node.js
- Python
Playwright
Scenario 2: PDF Opens in a New Tab
When a PDF is generated client-side and opens in a new tab, you can capture the PDF blob before it’s displayed:- Node.js
- Python
Playwright
How PDF Capture Works
Both scenarios use a combination of techniques:- Override
window.print()
- Prevents the native print dialog from blocking automation - Intercept
URL.createObjectURL()
- Captures client-side generated PDF blobs before they’re displayed - Route network requests (Scenario 1) - Intercepts PDF files loaded from the network
- Scenario 1: PDF might be fetched from the network or generated, and the print dialogue appears in the current tab
- Scenario 2: PDF is generated client-side and stored in a blob, opened in a new tab where the print dialogue appears