Web Automation

What is Puppeteer? (Complete Guide 2026)

What is Puppeteer? (Complete Guide 2026) — conceptual illustration
On this page

Puppeteer is a Node.js tool that lets your code drive a real Chrome browser automatically — clicking, typing, and reading pages just like a person would. This is a complete 2026 guide to what it is and how to use it.

Quick facts

What it isNode.js headless-Chrome library
ControlsChrome/Chromium via DevTools Protocol
Best forJS rendering, screenshots, PDFs
LanguageJavaScript / TypeScript
Cross-browser altPlaywright

What is Puppeteer?

Puppeteer is a Node.js library that gives you a simple API to control Chrome or Chromium from your code. It talks to the browser through the DevTools Protocol — the same behind-the-scenes channel Chrome's own developer tools use to inspect and command a page. Google's Chrome team maintains it, and it's a great fit for automating browsers, running tests, and scraping data.

Key Features

1. Browser Automation

const puppeteer = require('puppeteer');

async function automateWebsite() {
    // Launch browser
    const browser = await puppeteer.launch({
        headless: 'new',  // Use new headless mode
        defaultViewport: {width: 1920, height: 1080}
    });
    
    // Create new page
    const page = await browser.newPage();
    
    // Navigate to website
    await page.goto('https://example.com', {
        waitUntil: 'networkidle0'
    });
    
    // Close browser
    await browser.close();
}

The script above opens a browser (here in "headless" mode, meaning no visible window), opens a tab, loads a page, and waits until network traffic settles before closing.

2. Screenshot & PDF Generation

Because Puppeteer drives a real browser, it can also save what the page looks like — as an image or a PDF.

async function captureContent() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    // Take screenshot
    await page.screenshot({
        path: 'screenshot.png',
        fullPage: true
    });
    
    // Generate PDF
    await page.pdf({
        path: 'document.pdf',
        format: 'A4'
    });
    
    await browser.close();
}

Common Use Cases

1. Web Scraping

You can pull data out of a page by running code inside the browser. page.evaluate runs your function in the page's own JavaScript context, so it can read the live DOM (the page structure) and hand the result back to your script.

async function scrapeData() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    await page.goto('https://example.com');
    
    // Extract data
    const data = await page.evaluate(() => {
        const title = document.querySelector('h1').innerText;
        const paragraphs = Array.from(
            document.querySelectorAll('p')
        ).map(p => p.innerText);
        
        return { title, paragraphs };
    });
    
    console.log(data);
    await browser.close();
}

2. Form Automation

Puppeteer can also fill in and submit forms — typing into fields and clicking buttons for you.

async function fillForm() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    await page.goto('https://example.com/form');
    
    // Fill form fields
    await page.type('#username', 'testuser');
    await page.type('#password', 'password123');
    
    // Click submit button
    await Promise.all([
        page.waitForNavigation(),
        page.click('#submit-button')
    ]);
    
    await browser.close();
}

Best Practices

1. Resource Management

Each browser uses real memory and CPU, so always close it when you're done. Wrapping launch and close in a small class keeps that cleanup in one place. The launch flags below (like --no-sandbox) are common when running inside Docker or other Linux containers.

class PuppeteerManager {
    constructor() {
        this.browser = null;
    }
    
    async initialize() {
        this.browser = await puppeteer.launch({
            args: [
                '--no-sandbox',
                '--disable-setuid-sandbox',
                '--disable-dev-shm-usage'
            ]
        });
    }
    
    async cleanup() {
        if (this.browser) {
            await this.browser.close();
        }
    }
}

2. Error Handling

Pages fail, time out, or change without warning. Use try/finally so the browser always closes even when something breaks, and waitForSelector to pause until the element you need actually appears.

async function robustScraping() {
    let browser = null;
    try {
        browser = await puppeteer.launch();
        const page = await browser.newPage();
        
        // Set timeout for operations
        page.setDefaultTimeout(10000);
        
        await page.goto('https://example.com');
        
        // Wait for specific element
        await page.waitForSelector('.content');
        
    } catch (error) {
        console.error('Scraping failed:', error);
    } finally {
        if (browser) {
            await browser.close();
        }
    }
}

Performance Optimization

1. Resource Blocking

A full browser downloads images, stylesheets, fonts, and more. If you only need the text, you can cancel those requests to load pages much faster. Request interception lets you inspect each request and either abort (block) or continue (allow) it.

async function optimizedBrowsing() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    // Block unnecessary resources
    await page.setRequestInterception(true);
    page.on('request', (request) => {
        if (
            request.resourceType() === 'image' ||
            request.resourceType() === 'stylesheet'
        ) {
            request.abort();
        } else {
            request.continue();
        }
    });
    
    await page.goto('https://example.com');
    await browser.close();
}

2. Parallel Processing

To scrape many URLs faster, open several tabs in one browser and work through them at the same time instead of one after another.

async function parallelScraping(urls) {
    const browser = await puppeteer.launch();
    
    // Create multiple pages
    const pages = await Promise.all(
        Array(5).fill(null).map(() => browser.newPage())
    );
    
    // Process URLs in parallel
    const results = await Promise.all(
        urls.map((url, index) => {
            const page = pages[index % pages.length];
            return processUrl(page, url);
        })
    );
    
    await browser.close();
    return results;
}

Remember: Puppeteer is a powerful tool for web automation, but use it responsibly and respect websites' terms of service and robots.txt directives.

Related terms

Concept map

How Puppeteer? (Complete Guide 2026) connects

The terms most directly tied to this one. Hover a node to see its neighbours, click to preview, drag to rearrange.

0 terms · 0 connections
You are here · Web Automation
Building map…

Frequently asked questions

Puppeteer or Playwright?

Playwright is a newer tool from the same lineage that works across multiple browser engines (Chromium, Firefox, and WebKit, the engine behind Safari). It also waits for elements automatically and supports several programming languages. Puppeteer is Chrome-only and Node.js-only, but it's lighter if that's all you need.

Is Puppeteer detectable?

Yes. Default headless Chrome gives off telltale signs that it's automated rather than a real person. Stealth plugins hide some of those signs, but well-built anti-bot systems still catch it. Running a real browser or using a managed scraping service is harder to detect.

Can Puppeteer run with a visible browser?

Yes. Launch it with headless: false and you'll see a real Chrome window doing the work, which is handy when you're debugging which elements to click or why a flow breaks.

Last updated: 2026-05-31