Network Control & Advanced Patterns
Request Interception
await page.setRequestInterception(true);
page.on('request', (request) => {
// Every intercepted request MUST call exactly one of continue/abort/respond,
// or the request (and the page load) hangs forever.
const blocked = ['image', 'stylesheet', 'font'];
if (blocked.includes(request.resourceType())) {
request.abort(); // big speedup when you don't need visual rendering
} else if (request.url().includes('/analytics')) {
request.respond({ status: 200, body: '{}' }); // fake a response
} else {
request.continue();
}
});
// Wait for a specific response instead of a fixed delay
const response = await page.waitForResponse(
(res) => res.url().includes('/api/data') && res.status() === 200
);
const data = await response.json();
// Cookies — reuse an authenticated session across runs
const cookies = await page.cookies();
require('fs').writeFileSync('cookies.json', JSON.stringify(cookies));
// ...later, in a new run:
const saved = JSON.parse(require('fs').readFileSync('cookies.json'));
await page.setCookie(...saved);Dialogs, Frames & Console
// Native alert/confirm/prompt dialogs pause page JS until handled
page.on('dialog', async (dialog) => {
console.log(dialog.message());
await dialog.accept(); // or dialog.dismiss()
});
// Console messages from the page don't reach Node automatically
page.on('console', (msg) => console.log('PAGE LOG:', msg.text()));
// Iframes — get the Frame, then query/interact on it directly
const frameHandle = await page.$('iframe#checkout');
const frame = await frameHandle.contentFrame();
await frame.type('#card-number', '4242424242424242');
// Or find by name/url
const frame2 = page.frames().find(f => f.url().includes('checkout'));
// Exposing a Node function for page code to call back into
await page.exposeFunction('logToDisk', (text) => require('fs').appendFileSync('log.txt', text + '\n'));
await page.evaluate(() => window.logToDisk('page loaded'));Device Emulation & Debugging
const { KnownDevices } = require('puppeteer');
await page.emulate(KnownDevices['iPhone 13']);
// Throttle the network to test slow-connection behavior
const client = await page.createCDPSession();
await client.send('Network.emulateNetworkConditions', {
offline: false,
latency: 400,
downloadThroughput: (750 * 1024) / 8,
uploadThroughput: (250 * 1024) / 8,
});
// Performance trace for later analysis in Chrome DevTools
await page.tracing.start({ path: 'trace.json', screenshots: true });
// ...actions to trace...
await page.tracing.stop();
// Visible + slowed-down browser for local debugging
const debugBrowser = await puppeteer.launch({ headless: false, slowMo: 100, devtools: true });
// puppeteer-core: same API, no bundled Chromium download — for CI images
// or serverless environments that already provide a pinned Chrome build
const puppeteerCore = require('puppeteer-core');
const b = await puppeteerCore.launch({ executablePath: '/usr/bin/chromium' });Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free