User Agents

Web browsers and everyday web tech, explained

Replay Chrome Recorder Puppeteer Script in Python

· 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

Chrome DevTools Recorder lets you record browser actions and export them as a Puppeteer script. While the export is JavaScript, you can replay the same recorded flow in Python using Playwright, which has a nearly identical API to Puppeteer. This guide walks you through converting and running your recorded script in a Python environment, step by step.

What You'll Need

  • Python 3.7 or newer installed on your machine.
  • Google Chrome or Chromium browser (Playwright can also use its own bundled browsers).
  • Basic familiarity with the command line and Python package installation.

Step-by-Step

  1. Record the flow in Chrome. Open Chrome, press F12 to open DevTools, go to the Recorder tab, and click Record. Perform the actions you want to automate, then stop recording. Give the recording a name.
  2. Export the recording as a Puppeteer script. In the Recorder panel, click the export icon (downward arrow) and choose Puppeteer. Save the generated .js file somewhere accessible.
  3. Install Playwright and its browser. Open a terminal and run: pip install playwright. Then run playwright install chromium to download a compatible browser (or use your existing Chrome).
  4. Create a new Python file. For example, replay.py. Open it in your editor.
  5. Import Playwright and set up the browser launch. Start with:
    from playwright.sync_api import sync_playwright
    
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()
    This mirrors Puppeteer's puppeteer.launch() and browser.newPage().
  6. Translate the Puppeteer actions line by line. Open the exported .js file. Most actions have direct equivalents:
    • page.goto(url) → page.goto(url) (same name)
    • page.click(selector) → page.click(selector)
    • page.type(selector, text) → page.fill(selector, text) (or page.type if you need keystroke simulation)
    • page.waitForSelector(selector) → page.wait_for_selector(selector)
    • page.screenshot({path: 'file.png'}) → page.screenshot(path='file.png')
  7. Handle waits and assertions. Puppeteer scripts often include page.waitForTimeout(ms). In Playwright, use page.wait_for_timeout(ms) (though it's better to replace with explicit waits for stability). If your script has expect calls (from Puppeteer's expect library), you'll need to rewrite them using Python's assert or Playwright's expect from playwright.sync_api.
  8. Close the browser at the end. Add browser.close() after your actions, inside the with block.
  9. Run the script. In the terminal, execute python replay.py. Watch the browser perform the recorded steps.

Common Mistakes

  • Using the wrong browser type. If you recorded in Chrome, ensure you launch with p.chromium.launch() and not p.firefox or p.webkit.
  • Forgetting to install the browser. Running playwright install chromium is essential; otherwise, you'll get a "browser executable not found" error.
  • Copying JavaScript syntax directly. Puppeteer uses camelCase methods like waitForSelector, but Playwright Python uses snake_case: wait_for_selector. Watch for these differences.
  • Using page.type for non-input elements. page.fill is more reliable for text inputs; page.type simulates key presses and may fail if the element isn't focusable.
  • Not handling dynamic waits. If the original script used fixed waitForTimeout, the Python version may break if the page loads slower. Prefer wait_for_selector or wait_for_load_state.

Tips

  • Use Playwright's codegen to verify selectors. Run playwright codegen to open a browser and generate Python code for the same actions; this can help you confirm the correct selector syntax.
  • Keep the headless setting flexible. Use headless=False while debugging to see what's happening, then switch to headless=True for production.
  • Leverage Playwright's auto-waiting. Playwright automatically waits for elements to be actionable before clicking, which often makes the script more robust than the original Puppeteer script.
  • Store selectors as variables. If you have many actions, define selectors at the top of the script for easier maintenance.
  • Add screenshots at key steps. Insert page.screenshot(path='step1.png') to debug where the script fails.

Troubleshooting

  • Error: "Executable doesn't exist" — Run playwright install chromium again, or specify the path to your Chrome with executable_path in launch().
  • Script works in JS but not Python — Check for case differences (e.g., newPage vs new_page) and argument styles (keyword vs positional).
  • Element not found or not visible — Use page.wait_for_selector(selector, state='visible') before interacting. Also verify the selector is correct by inspecting the page in DevTools.
  • Script runs but does nothing — Ensure you're calling page.goto() with the full URL (including https://). Also check for pop-ups or new tabs that might redirect.
  • Timeout errors — Increase the default timeout by passing timeout=10000 to methods, or set a global timeout with page.set_default_timeout(10000).
  • Cannot find module 'playwright' — Make sure you installed it in the same Python environment you're running the script from. Use pip list to verify.

Frequently Asked Questions

Do I need to install Puppeteer to replay the script in Python?

No. You only need the exported JavaScript file as a reference. Python uses Playwright, which is a separate library.

Can I convert the Puppeteer script automatically to Python?

There is no official converter. Manual translation is straightforward for most actions, but complex logic (loops, conditionals) requires rewriting.

Will the Python script work with the same Chrome browser I recorded in?

Yes, Playwright can launch your installed Chrome by specifying executable_path. Otherwise, it uses its own Chromium build.

What if my recorded script uses custom Puppeteer functions?

You'll need to replicate those functions in Python. For example, if you used page.evaluate(), you can use page.evaluate() in Playwright Python with the same JavaScript string.

Is Playwright Python faster than Puppeteer?

Performance is comparable. Both are built on the Chrome DevTools Protocol. The main difference is language preference.

Can I run the script on a headless server?

Yes, set headless=True in the launch options. Ensure the server has the necessary system dependencies for Chromium, which Playwright can install via playwright install-deps.