Replay Chrome Recorder Puppeteer Script in Python
In this article
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
- Record the flow in Chrome. Open Chrome, press
F12to 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. - Export the recording as a Puppeteer script. In the Recorder panel, click the export icon (downward arrow) and choose Puppeteer. Save the generated
.jsfile somewhere accessible. - Install Playwright and its browser. Open a terminal and run:
pip install playwright. Then runplaywright install chromiumto download a compatible browser (or use your existing Chrome). - Create a new Python file. For example,
replay.py. Open it in your editor. - Import Playwright and set up the browser launch. Start with:
This mirrors Puppeteer'sfrom playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) page = browser.new_page()puppeteer.launch()andbrowser.newPage(). - Translate the Puppeteer actions line by line. Open the exported
.jsfile. 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)(orpage.typeif you need keystroke simulation)page.waitForSelector(selector)→page.wait_for_selector(selector)page.screenshot({path: 'file.png'})→page.screenshot(path='file.png')
- Handle waits and assertions. Puppeteer scripts often include
page.waitForTimeout(ms). In Playwright, usepage.wait_for_timeout(ms)(though it's better to replace with explicit waits for stability). If your script hasexpectcalls (from Puppeteer's expect library), you'll need to rewrite them using Python'sassertor Playwright'sexpectfromplaywright.sync_api. - Close the browser at the end. Add
browser.close()after your actions, inside thewithblock. - 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 notp.firefoxorp.webkit. - Forgetting to install the browser. Running
playwright install chromiumis 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.typefor non-input elements.page.fillis more reliable for text inputs;page.typesimulates 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. Preferwait_for_selectororwait_for_load_state.
Tips
- Use Playwright's codegen to verify selectors. Run
playwright codegento 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=Falsewhile debugging to see what's happening, then switch toheadless=Truefor 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 chromiumagain, or specify the path to your Chrome withexecutable_pathinlaunch(). - Script works in JS but not Python — Check for case differences (e.g.,
newPagevsnew_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 (includinghttps://). Also check for pop-ups or new tabs that might redirect. - Timeout errors — Increase the default timeout by passing
timeout=10000to methods, or set a global timeout withpage.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 listto 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.