Puppeteer with Claude Code: Browser Automation Guide
In this article
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
- Install Puppeteer in your project
Open your terminal, navigate to your project folder, and runnpm init -yto create a package.json file. Then install Puppeteer withnpm install puppeteer. This will download Chromium automatically. - Start Claude Code
Runclaudein your terminal to start an interactive session. If you haven't set it up, follow the on-screen instructions to authenticate. - 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. - Let Claude generate the script
Claude Code will generate a JavaScript file based on your description. It will likely create a file likescript.jsin your project folder. Review the code to ensure it matches your intent. - Run the script
Execute the script withnode script.js. If the script uses async/await, it will run as expected. Check the output for errors or results. - 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. - 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." - 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 usesawaitfor 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 usepage.waitForSelector()or similar waits. - Forgetting to close the browser
Always callbrowser.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-sandboxflag or run as a regular user.
Tips
- Use headless mode for speed
By default, Puppeteer runs headless. If you need to see the browser, setheadless: falsein 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 runningnpx puppeteer browsers install chrometo ensure the browser is downloaded. If you're on a server, you may need to install system dependencies likelibnss3andlibatk. - Timeouts when waiting for elements
Increase the timeout inpage.waitForSelector()or usepage.waitForNavigation()for page loads. Also check if the element is in an iframe. - Script runs but returns empty data
Inspect the page content withconsole.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 runclaudefrom the project directory. - Browser opens but closes immediately
Check if you havebrowser.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.