What are Chrome dialogs?
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 can’t interact with the page while a dialog is open
- Automation scripts will hang until the dialog is manually dismissed
Preventing Chrome dialogs
The best approach is to prevent these dialogs from appearing by overriding the native dialog functions before any page code runs. UseaddInitScript(), 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 dialogs
PDF dialogs present unique challenges because PDFs can be generated and displayed in different ways. You’ll encounter two common scenarios: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 dialog 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 dialog appears