User Agents

Web browsers and everyday web tech, explained

Screenshot a Chrome Extension Page with Puppeteer

· 5 min read · Extensions

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to take a screenshot of a Chrome extension's page (like its popup, options page, or a page it opens in a tab) using Puppeteer. Puppeteer normally launches a clean Chrome instance, so you need to load the extension manually. By the end, you'll have a PNG file saved to your disk.

What You'll Need

  • Node.js (v14 or later) installed on your machine.
  • Puppeteer installed in your project: npm install puppeteer
  • The path to your extension's folder (the one containing manifest.json).
  • Basic familiarity with JavaScript and running scripts via the terminal.

Step-by-Step

  1. Set up your script file. Create a new file, e.g., screenshot.js, in your project directory.
  2. Require Puppeteer and define your extension path. At the top of the file, add:
    const puppeteer = require('puppeteer');
    const path = require('path');
    const extensionPath = path.resolve('/absolute/path/to/your/extension');
    Replace the path with the actual location of your extension folder.
  3. Launch a browser with the extension loaded. Use the following code to start Chrome with your extension enabled:
    const browser = await puppeteer.launch({
      headless: false, // Extensions work only in headful mode
      args: [
        `--disable-extensions-except=${extensionPath}`,
        `--load-extension=${extensionPath}`
      ]
    });
    Note: headless: false is required because Puppeteer's headless mode (old headless) does not support extensions. If you use Puppeteer v22+ with the new headless, you can set headless: 'new' and it may work, but headful is safer.
  4. Create a new page. Extensions often have a popup or options page. You can open it directly via its URL. First, get the extension ID. When you launch with --load-extension, the ID is generated based on the path. To find it, you can use chrome://extensions manually, but in code you can query the service workers or just guess if you know the ID. A simpler approach: open a normal page first, then navigate to chrome-extension://[extension-id]/popup.html (or whatever page you need). To get the ID programmatically, use the targets() method after launch:
    const targets = await browser.targets();
    const extensionTarget = targets.find(t => t.type() === 'service_worker' && t.url().startsWith('chrome-extension://'));
    const extensionId = new URL(extensionTarget.url()).hostname;
    console.log('Extension ID:', extensionId);
    If your extension doesn't have a service worker, you can find the ID by listing all targets and looking for any chrome-extension:// URL.
  5. Open the extension page. Using the ID, construct the URL and open it in a new page:
    const page = await browser.newPage();
    await page.goto(`chrome-extension://${extensionId}/popup.html`, { waitUntil: 'networkidle0' });
    Replace popup.html with the actual page you want (e.g., options.html). If you're not sure which page exists, check your extension's manifest.json for the default_popup or options_page fields.
  6. Wait for content to load. If your extension fetches data or renders asynchronously, add a delay or wait for a selector:
    await page.waitForSelector('#some-element'); // or
    await new Promise(r => setTimeout(r, 2000));
  7. Take the screenshot. Capture the full page or just the visible area:
    await page.screenshot({ path: 'extension-screenshot.png', fullPage: true });
    For only the viewport, omit fullPage.
  8. Close the browser. Always close to free resources:
    await browser.close();
  9. Run the script. In your terminal, execute node screenshot.js. The PNG file will be saved in the same directory as your script (or the path you specified).

Common Mistakes

  • Using headless: true – Extensions don't load in classic headless mode. You'll get a blank page or an error.
  • Incorrect extension path – The path must point to the folder with manifest.json, not a parent or child folder.
  • Forgetting to wait – Screenshotting before the page finishes rendering yields a blank or partial image.
  • Hardcoding the extension ID – The ID changes if you move the folder or run on a different machine. Always derive it dynamically.
  • Not closing the browser – Leaves orphan processes that can interfere with subsequent runs.

Tips

  • Set a custom viewport to match the popup's dimensions: await page.setViewport({ width: 400, height: 600 });
  • Use waitUntil: 'networkidle0' for pages that load external resources. For single-page apps, wait for a specific element instead.
  • If your extension uses a popup that closes when it loses focus, consider opening the popup as a normal tab by navigating to its URL directly (as shown). This avoids the focus issue.
  • For multiple screenshots, loop over different URLs or viewport sizes, but remember to reset the viewport each time.
  • Use --user-data-dir to persist extension state if you need to be logged in, but be careful not to reuse a profile with conflicting extensions.

Troubleshooting

  • Blank page or “chrome-extension://” not found – Ensure the extension is actually loaded. Check the browser's chrome://extensions page manually by launching with headless: false and inspecting. Also verify the page file exists in your extension folder.
  • Extension ID not found in targets – If your extension has no service worker (e.g., a simple popup-only extension), look for a target of type other or page that starts with chrome-extension://. You can also print all targets to see what's there.
  • Puppeteer throws “Failed to load extension” – Double-check the path. On Windows, use forward slashes or escaped backslashes. Also ensure manifest.json is valid JSON.
  • Screenshot is blurry or wrong size – Set the viewport before navigating, and use deviceScaleFactor: 1 (default) for crisp text. For high-DPI, set deviceScaleFactor: 2.
  • Script hangs – If networkidle0 never fires (e.g., a persistent WebSocket), use waitUntil: 'domcontentloaded' plus a fixed delay.
  • Browser closes before screenshot – Ensure you await the screenshot call and don't accidentally call browser.close() too early.

Frequently Asked Questions

Can I take a screenshot of the extension popup without opening it as a tab?

Yes, but it's tricky. You can simulate clicking the toolbar icon, but that requires more complex input handling. The simplest way is to open the popup's URL directly in a tab, as shown above.

Does this work with Chrome extensions that use a background script?

Yes, the background script (service worker) loads as part of the extension. You can access its console via the target, but for screenshots you only need to open the page you want to capture.

Can I use Puppeteer's headless mode?

Classic headless mode (headless: true) doesn't support extensions. In Puppeteer v22+, you can try headless: 'new', but it's not guaranteed. Use headful mode for reliability.

How do I find the extension ID if my extension doesn't have a service worker?

List all targets and look for any URL starting with chrome-extension://. For example, a background page or a popup page might appear as a target. You can also manually check chrome://extensions in the launched browser.

Can I screenshot a page that requires user interaction (like a login form)?

Yes, you can use Puppeteer's page.type() and page.click() to fill in fields before taking the screenshot. Just add those steps between navigation and screenshot.

Is there a way to capture the entire extension UI, including the toolbar icon?

No, Puppeteer cannot screenshot the browser chrome (toolbar). It only captures web content. To capture the icon, you'd need a different tool or a manual screenshot.