User Agents

Web browsers and everyday web tech, explained

Generate Puppeteer Scripts with Chrome Recorder

· 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

Generate Puppeteer scripts with Chrome Recorder by capturing your browser actions and exporting them as executable Node.js code. This feature, available in Chrome DevTools, allows developers and testers to automate web interactions—like form submissions, navigation, and data extraction—without manually writing Puppeteer code. By recording a user flow, Chrome converts browser events into a script compatible with Puppeteer, speeding up test creation and reducing errors from hand-coding selectors.

What You'll Need

  • Google Chrome (version 120 or later recommended)
  • A website to interact with (any public site or local development server)
  • Basic knowledge of JavaScript and Node.js
  • Node.js installed on your system to run exported scripts
  • Chrome DevTools access (built into Chrome)

Step-by-Step

  1. Open Google Chrome and navigate to the webpage you want to automate. Ensure the site loads completely and you're logged in if necessary.
  2. Open Chrome DevTools by pressing F12 or Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac).
  3. In DevTools, click the Recorder tab. If you don’t see it, click >> (More Tools) in the tab bar and select Recorder from the dropdown.
  4. Click Create recording and give your session a descriptive name (e.g., "Login Flow" or "Form Submission"). Press Enter.
  5. Click the red Record button to begin capturing actions. A small control panel will appear in the top-right of the browser window.
  6. Perform the actions you want to automate—clicking buttons, typing in fields, navigating links. Avoid unnecessary movements; keep actions focused and sequential.
  7. When done, click the Stop button on the control panel to end the recording.
  8. Review the recorded steps in the Recorder panel. Each action appears as a discrete event (e.g., "Click on 'Sign In'", "Type 'example@email.com'"). You can delete or reorder steps if needed.
  9. Click the three-dot menu next to the recording name and select Export. Choose Puppeteer from the export options.
  10. Chrome will generate a JavaScript file with Puppeteer code. Copy this code or download it as a .js file.
  11. Create a new Node.js project or navigate to your project directory in the terminal. Run npm init -y if starting fresh.
  12. Install Puppeteer by running npm install puppeteer.
  13. Paste the exported script into a new file (e.g., automation.js). Ensure the script uses await properly and is inside an async function if needed.
  14. Run the script using node automation.js. The browser should launch and replay your recorded actions.
  15. Verify the script works. If it fails, check selectors or timing—adjust manually in the code as needed.

Common Mistakes

  • Recording on a slow or unstable network: Delays can cause Puppeteer to miss elements. Always record on a stable connection and consider adding await page.waitForSelector() calls.
  • Using dynamic content without waits: Pages with JavaScript rendering (e.g., React apps) may not load elements in time. The recorder doesn’t auto-insert waits, so add them manually.
  • Recording too many actions: Long recordings increase failure risk. Break complex flows into smaller scripts (e.g., login, search, checkout).
  • Ignoring authentication state: Exported scripts don’t preserve login sessions. Use await page.goto() with session cookies or include login steps.
  • Overlooking selector fragility: Chrome Recorder often uses XPath or CSS paths that break if the page structure changes. Prefer stable selectors like [data-testid] when possible.

Tips

  • Add await page.waitForTimeout(1000); between critical steps if the site is slow, though use waitForSelector instead when possible.
  • Use console.log statements in your Puppeteer script to debug execution flow.
  • Run scripts in non-headless mode initially by setting headless: false in puppeteer.launch() to see what’s happening.
  • Name recordings clearly to distinguish between different user journeys (e.g., "Add to Cart", "Search Product").
  • Test exported scripts on different environments (CI/CD, local, Docker) to catch platform-specific issues.
  • Update Chrome regularly—new versions improve Recorder stability and Puppeteer compatibility.

Troubleshooting

If the exported script fails to run, first check the error message. Common issues include:

  • Element not found: The selector may have changed. Open DevTools, inspect the current element, and update the selector in the script.
  • Timeout errors: Increase default timeout with await page.setDefaultTimeout(60000); at the start of your script.
  • Navigation fails: Ensure the initial page.goto() URL matches the domain and protocol (e.g., HTTPS vs HTTP).
  • Recorder not visible: If the Recorder tab is missing, ensure Chrome is updated. Some enterprise policies disable DevTools features.
  • Export option grayed out: Make sure you’ve stopped the recording and selected a valid recording from the list.
  • Script runs but doesn’t interact: Run in non-headless mode to confirm actions are being triggered. Some sites block automated behavior—check userAgent settings.

Frequently Asked Questions

Can Chrome Recorder generate Puppeteer scripts for any website?

Yes, but sites with anti-bot measures or complex SPAs may require manual adjustments to selectors or timing.

Does the Recorder work with iframes or shadow DOM?

Limited support. You may need to manually switch frames or query shadow roots in the exported code.

Can I edit the generated Puppeteer script?

Yes—edit timeouts, selectors, and add logic like loops or conditionals as needed.

Is Chrome Recorder available in Chromium-based browsers?

Only Chrome has the built-in Recorder with Puppeteer export. Edge or Brave do not support this feature.

Do I need to install anything besides Chrome and Node.js?

No—Puppeteer is the only required npm package. Chrome Recorder itself is built into DevTools.

Can I record file uploads or downloads?

File uploads can be recorded, but downloads require manual setup in Puppeteer using page.waitForEvent('download').