User Agents

Web browsers and everyday web tech, explained

Puppeteer with Claude Code: Browser Automation Guide

· 4 min read · Browsers

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 use Puppeteer with Claude Code to automate browser tasks like scraping, form filling, and testing. Claude Code can write and run Puppeteer scripts based on natural language instructions, making browser automation faster and more accessible. You'll need Node.js, a basic understanding of JavaScript, and access to Claude Code (Anthropic's CLI tool) installed on your machine.

What You'll Need

  • Node.js (v14 or later) installed
  • Claude Code CLI installed and authenticated
  • Basic knowledge of JavaScript and command line
  • A code editor (optional but helpful)

Step-by-Step

  1. Install Puppeteer in your project
    Open your terminal, navigate to your project folder, and run npm init -y to create a package.json file. Then install Puppeteer with npm install puppeteer. This will download Chromium automatically.
  2. Start Claude Code
    Run claude in your terminal to start an interactive session. If you haven't set it up, follow the on-screen instructions to authenticate.
  3. Describe your automation task
    In Claude Code, type a clear description of what you want to automate. For example: "Write a Puppeteer script that opens example.com, extracts all the headings, and saves them to a text file." Be specific about the URL, actions, and output format.
  4. Let Claude generate the script
    Claude Code will generate a JavaScript file based on your description. It will likely create a file like script.js in your project folder. Review the code to ensure it matches your intent.
  5. Run the script
    Execute the script with node script.js. If the script uses async/await, it will run as expected. Check the output for errors or results.
  6. Iterate and refine
    If the script doesn't work as expected, tell Claude Code what went wrong. For example: "The selector for the button is wrong, it should be .submit-btn." Claude will update the script accordingly.
  7. Handle dynamic content
    If the page loads content dynamically, ask Claude to add waits and timeouts. For instance: "Wait for the element with id 'results' to appear before scraping."
  8. Save and reuse
    Once the script works, save it in your project. You can run it again manually or schedule it with cron jobs.

Common Mistakes

  • Not handling async properly
    Puppeteer is asynchronous. Make sure your script uses await for all Puppeteer calls, and the main function is wrapped in an async function.
  • Using wrong selectors
    Selectors must match the page structure. Use browser DevTools to inspect elements and copy the correct selector.
  • Ignoring page load times
    If you try to interact with elements before they load, you'll get errors. Always use page.waitForSelector() or similar waits.
  • Forgetting to close the browser
    Always call browser.close() at the end of your script to free resources.
  • Running Puppeteer as root
    If you're on Linux and running as root, Puppeteer may fail. Use --no-sandbox flag or run as a regular user.

Tips

  • Use headless mode for speed
    By default, Puppeteer runs headless. If you need to see the browser, set headless: false in the launch options.
  • Leverage Claude's code review
    After Claude generates a script, ask it to review for potential issues or optimizations.
  • Break complex tasks into steps
    Instead of asking for a huge script at once, break it down. First, open a page, then extract data, then save.
  • Use environment variables for sensitive data
    If your script needs login credentials, store them in environment variables rather than hardcoding.
  • Test with a small sample first
    Run the script on a single page before scaling to many URLs.

Troubleshooting

  • Puppeteer fails to launch
    Try running npx puppeteer browsers install chrome to ensure the browser is downloaded. If you're on a server, you may need to install system dependencies like libnss3 and libatk.
  • Timeouts when waiting for elements
    Increase the timeout in page.waitForSelector() or use page.waitForNavigation() for page loads. Also check if the element is in an iframe.
  • Script runs but returns empty data
    Inspect the page content with console.log(await page.content()) to see if the selector matches anything. Adjust the selector accordingly.
  • Claude Code gives an error about permissions
    Make sure Claude has read/write access to your project folder. You may need to run claude from the project directory.
  • Browser opens but closes immediately
    Check if you have browser.close() inside the script before it finishes. Move it to the end.

Frequently Asked Questions

Can Claude Code write Puppeteer scripts from scratch?

Yes, you can describe the automation in plain English, and Claude Code will generate a working Puppeteer script. It's best to be specific about the steps and selectors.

Do I need to install Puppeteer separately?

Yes, Puppeteer is a Node.js library that needs to be installed in your project. Claude Code will not install it for you.

Can I use Puppeteer with Claude Code to scrape websites that require login?

Yes, you can automate login by filling in form fields and clicking submit. Just be aware of the website's terms of service and login policies.

Is Puppeteer the only option for browser automation with Claude Code?

No, you can also use Playwright or Selenium, but Puppeteer is popular and well-supported. Claude Code can generate scripts for any library you specify.

How do I handle CAPTCHAs in Puppeteer scripts?

Puppeteer cannot solve CAPTCHAs automatically. You can pause the script and let a human solve it, or use a CAPTCHA-solving service (if allowed).

Can I run Puppeteer scripts in the cloud?

Yes, you can run them on any Node.js environment, including cloud servers. You may need to install additional dependencies for headless Chrome.