Skip to main content

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” button
  • confirm() dialogs that ask for confirmation with “OK” and “Cancel” buttons
  • prompt() dialogs that request text input from the user
  • window.print() dialogs that open the browser’s print interface
These native browser dialogs are different from custom web page modals and require special handling in automation.

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 using addInitScript(), which executes code before any page scripts.

Implementation

  • Node.js
  • Python
Playwright
import { chromium } from "playwright-core";
import { Browserbase } from "@browserbasehq/sdk";

const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID
});

const browser = await chromium.connectOverCDP(session.connectUrl);
const defaultContext = browser.contexts()[0];
const page = defaultContext.pages()[0];

// Override dialog functions before page loads
await page.context().addInitScript(() => {
  window.alert = () => {};
  window.confirm = () => true;
  window.prompt = () => '';
});

// Now navigate to your target page
await page.goto("https://example.com");

// Dialogs should now be prevented

await page.close();
await browser.close();

Customizing Dialog Behavior

You can customize the return values based on your automation needs:
// Always reject confirmations
window.confirm = () => false;

// Return a specific value for prompts
window.prompt = () => 'my custom input';

// Log when dialogs would have appeared
window.alert = (message) => console.log('Alert prevented:', message);

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
import { chromium } from "playwright-core";
import { Browserbase } from "@browserbasehq/sdk";
import fs from 'fs/promises';

const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID
});

const browser = await chromium.connectOverCDP(session.connectUrl);
const context = browser.contexts()[0];
const page = context.pages()[0];

let savedPdfPath: string | null = null;

// Route PDF requests to capture them
await context.route('**/*.pdf', async (route) => {
  const resp = await route.fetch();
  const contentType = (resp.headers()['content-type'] ?? '').toLowerCase();

  if (contentType.includes('application/pdf')) {
    const url = route.request().url();
    const name = new URL(url).pathname.split('/').pop() || 'document.pdf';
    const outputPath = `captured-${Date.now()}-${name}`;
    const buffer = await resp.body();

    await fs.writeFile(outputPath, buffer);
    console.log('Saved PDF (network):', outputPath, 'from', url);
    savedPdfPath = outputPath;
  }

  await route.fulfill({ response: resp });
});

// Override print dialog
await context.addInitScript(() => {
  window.print = () => {};

  // Also capture client-side blobs as fallback
  const origCreate = URL.createObjectURL;
  (window as any).__pdfBlobs = [];
  URL.createObjectURL = function (blob: Blob) {
    try {
      if ((blob as any)?.type?.toLowerCase?.().includes('application/pdf')) {
        (window as any).__pdfBlobs.push(blob);
      }
    } catch {}
    return origCreate.apply(this, arguments);
  };
});

await page.goto("https://dialogue-interaction.vercel.app/");

// Trigger PDF generation
await page.click("#open-pdf");

// If network capture didn't work, try blob capture
if (!savedPdfPath) {
  const arrays = await page.evaluate(async () => {
    const blobs: Blob[] = (window as any).__pdfBlobs || [];
    const bufs = await Promise.all(blobs.map(b => b.arrayBuffer()));
    return bufs.map(ab => Array.from(new Uint8Array(ab)));
  });

  if (arrays.length) {
    const buf = Buffer.from(Uint8Array.from(arrays[0]));
    const outputPath = `captured-blob-${Date.now()}.pdf`;
    await fs.writeFile(outputPath, buf);
    console.log('Saved PDF (blob):', outputPath);
    savedPdfPath = outputPath;
  }
}

await page.close();
await browser.close();

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
import { chromium } from "playwright-core";
import { Browserbase } from "@browserbasehq/sdk";
import fs from 'fs/promises';

const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY });
const session = await bb.sessions.create({
  projectId: process.env.BROWSERBASE_PROJECT_ID
});

const browser = await chromium.connectOverCDP(session.connectUrl);
const context = browser.contexts()[0];
const page = context.pages()[0];

// Prevent print dialog and capture PDF blobs
await context.addInitScript(() => {
  // Don't let native print dialog block
  window.print = () => {};

  // Capture client-side generated PDF blobs
  const origCreate = URL.createObjectURL;
  (window as any).__pdfBlobs = [];
  URL.createObjectURL = function (blob: Blob) {
    try {
      if ((blob as any)?.type?.toLowerCase?.().includes('application/pdf')) {
        (window as any).__pdfBlobs.push(blob);
      }
    } catch {}
    return origCreate.apply(this, arguments);
  };
});

await page.goto("http://pdfmake.org/playground.html");

// Trigger PDF generation by clicking the print button
await page.click("#printThis li:nth-child(2)");

// Wait for PDF to be generated
await page.waitForTimeout(1000);

// Extract and save the PDF
const arrays = await page.evaluate(async () => {
  const blobs: Blob[] = (window as any).__pdfBlobs || [];
  const bufs = await Promise.all(blobs.map(b => b.arrayBuffer()));
  return bufs.map(ab => Array.from(new Uint8Array(ab)));
});

if (arrays.length) {
  const buf = Buffer.from(Uint8Array.from(arrays[0]));
  const outputPath = `captured-blob-${Date.now()}.pdf`;
  await fs.writeFile(outputPath, buf);
  console.log('Saved PDF (blob):', outputPath);
}

await page.close();
await browser.close();

How PDF Capture Works

Both scenarios use a combination of techniques:
  1. Override window.print() - Prevents the native print dialog from blocking automation
  2. Intercept URL.createObjectURL() - Captures client-side generated PDF blobs before they’re displayed
  3. Route network requests (Scenario 1) - Intercepts PDF files loaded from the network
The key difference:
  • 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
I