Data Science · Playwright
Why Every Data Scientist Should Learn Playwright in 2026
Quick summary — why data scientists should learn Playwright
Data scientists should learn Playwright in 2026 to extend Python into the browser: collecting permitted data from JavaScript-rendered pages, testing model demo apps end to end so stakeholders never see a broken form, and capturing report screenshots automatically, always checking terms of use, avoiding personal data and keeping request rates polite.
In this guide you will learn:
- Collecting permitted data — from rendered pages.
- Testing model demo apps — end to end.
- Automated screenshots — for reports.
- Responsible automation — rules.
- Interview questions — on data collection and testing.
SECTION 01Collecting permitted data from rendered pages
Simple HTTP requests only see a page's initial HTML, but many modern sites load data with JavaScript after the page opens. Playwright runs a real browser, waits for content to appear and lets you read it. Before collecting any web data, prefer official APIs or downloads, read the site's terms and robots.txt, avoid personal data and keep your request rate low:
| Step | Playwright tool | Purpose | Responsible practice |
|---|---|---|---|
| Open the page | page.goto | Load like a browser | Only permitted pages |
| Wait for content | locator.wait_for or expect | Data rendered by JavaScript | No hammering with retries |
| Extract | locator.all_inner_texts | Read values | Collect only needed fields |
| Pace requests | time.sleep between pages | Avoid overloading servers | Record source and date |
# Local page that renders a table with JavaScript, so no external site is used.
import pandas as pd
from playwright.sync_api import sync_playwright
HTML = """
<div id='app'>Loading...</div>
<script>
setTimeout(() => {
document.getElementById('app').innerHTML =
'<table id=t><tr><th>city</th><th>aqi</th></tr>' +
'<tr><td>Delhi</td><td>180</td></tr><tr><td>Noida</td><td>165</td></tr></table>';
}, 500);
</script>"""
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.set_content(HTML)
page.locator("#t").wait_for() # wait for JavaScript to render
rows = [r.locator("th, td").all_inner_texts() for r in page.locator("#t tr").all()]
browser.close()
df = pd.DataFrame(rows[1:], columns=rows[0]).astype({"aqi": int})
print(df) # note: sample values for practice only
Before collecting web data for a project:
[ ] Is there an official API or download? Use it instead.
[ ] Do the terms of use allow automated access?
[ ] Does robots.txt disallow these pages?
[ ] Does the data include personal information? If yes, stop and check law and policy.
[ ] Is the rate slow (pauses between pages)?
[ ] Have I recorded source URLs and collection dates?
[ ] Will I share only what I am allowed to share?
SECTION 02Testing model demo apps end to end
Data scientists often build small demo apps, with Streamlit, Gradio, Flask or Django, so stakeholders can try a model. A broken demo in front of managers is embarrassing. Playwright can test the whole flow automatically: open the app, fill inputs, submit and check that a sensible prediction appears. Running this test before every demo or in CI keeps your app reliable:
| Check | Playwright step | Catches | Tip |
|---|---|---|---|
| App loads | goto and title check | Server not running | Start the app before tests |
| Inputs work | fill and click | Broken form fields | Use labels for locators |
| Prediction shown | expect text pattern | Model or API errors | Check format, not exact value |
| Bad input handled | Submit invalid values | Crashes on edge cases | Expect a clear message |
# app.py pip install flask Run: python app.py
from flask import Flask, request
app = Flask(__name__)
FORM = '''<form method=post>
<label>Tenure months <input name=tenure></label>
<button>Predict</button></form><p id=result>{}</p>'''
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
try:
tenure = float(request.form["tenure"])
except ValueError:
return FORM.format("Please enter a number")
risk = 0.8 if tenure < 6 else 0.2 # stand-in for a model
return FORM.format(f"Churn risk: {risk:.2f}")
return FORM.format("")
if __name__ == "__main__":
app.run(port=5000)
# test_demo.py pip install pytest-playwright && playwright install chromium
# Start app.py first, then run: pytest -q
import re
from playwright.sync_api import Page, expect
URL = "http://localhost:5000/"
def test_prediction_appears(page: Page):
page.goto(URL)
page.get_by_label("Tenure months").fill("3")
page.get_by_role("button", name="Predict").click()
expect(page.locator("#result")).to_have_text(re.compile(r"Churn risk: 0\.\d{2}"))
def test_invalid_input_message(page: Page):
page.goto(URL)
page.get_by_label("Tenure months").fill("abc")
page.get_by_role("button", name="Predict").click()
expect(page.locator("#result")).to_have_text("Please enter a number")
SECTION 03Automated screenshots for reports
Weekly reports often include the same dashboard charts or app views. Playwright can open each page, wait for charts to load and save screenshots with dated file names, at a consistent size. This saves time and keeps reports consistent. It also works for checking how your own visualisations look at different screen sizes before sharing them:
| Task | Playwright option | Benefit | Tip |
|---|---|---|---|
| Full-page capture | screenshot(full_page=True) | Whole report in one image | Wait for charts first |
| Element capture | locator.screenshot | Just one chart | Use a stable selector |
| Consistent size | viewport setting | Same layout each week | Pick one size and keep it |
| Mobile check | Device emulation | See phone layout | Check text is readable |
from datetime import date
from pathlib import Path
from playwright.sync_api import sync_playwright
HTML = "<h1>Weekly report</h1><div id='chart' style='width:400px;height:200px;background:#cde'>chart</div>"
out = Path("screenshots")
out.mkdir(exist_ok=True)
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page(viewport={"width": 1280, "height": 800})
page.set_content(HTML) # or page.goto(your_dashboard_url) when authorised
page.locator("#chart").screenshot(path=out / f"chart_{date.today()}.png")
page.screenshot(path=out / f"report_{date.today()}.png", full_page=True)
browser.close()
print(sorted(p.name for p in out.iterdir()))
from playwright.sync_api import sync_playwright
HTML = "<h1>Model results</h1><p>Churn risk by plan</p><table><tr><td>Basic</td><td>0.42</td></tr></table>"
with sync_playwright() as p:
browser = p.chromium.launch()
phone = p.devices["iPhone 13"]
context = browser.new_context(**phone)
page = context.new_page()
page.set_content(HTML)
page.screenshot(path="mobile_view.png", full_page=True)
browser.close()
print("saved mobile_view.png")
SECTION 04Playwright data science project plan
Use these templates to build a project that combines Playwright with data science:
Project: Tested model demo with automated reporting
- Flask or Streamlit demo app serving a simple model
- Playwright end-to-end tests: valid input, invalid input, page loads
- GitHub Actions running the tests on every push
- Script capturing weekly screenshots of results pages
- Optional: permitted data collection from a local or open-data page
- README covering responsible data use and how to run tests
Week 1 Playwright basics: goto, locators, fill, click, expect
Week 2 End-to-end tests for your model demo; run in CI
Week 3 Screenshots, optional permitted data collection, README
Situation: a model demo occasionally broke before meetings
Task: make the demo reliable (practice project)
Action:
- Playwright tests for the full prediction flow
- tests run automatically in CI
- screenshots captured for weekly reports
Result: demo checked automatically; I can explain each test
SECTION 05Tips for data scientists learning Playwright
These habits keep Playwright useful and responsible:
- Prefer APIs and downloads — use browsers only when needed.
- Respect terms and privacy — never collect personal data casually.
- Test your demos — before every stakeholder session.
- Use label and role locators — they survive layout changes.
- Wait for content — avoid fixed sleeps where possible.
SECTION 06Interview Q&A — data collection and testing
Q1When would you use a browser tool instead of requests?
When content is rendered by JavaScript or needs interaction, and only when no official API or download exists and use is permitted.
Q2What should you check before collecting web data?
Terms of use, robots.txt, whether personal data is involved, legal requirements and whether an official source exists.
Q3How would you test a model demo app?
Write end-to-end tests that load the app, enter valid and invalid inputs, submit and check that results or error messages appear correctly.
Q4Why avoid fixed sleeps in browser automation?
They are slow and unreliable; waiting for specific elements or conditions is faster and more robust.
Q5How do you record data provenance?
Store the source URL, collection date, method and any permissions or terms alongside the dataset.
SECTION 07Test yourself — data collection and testing quiz
Five questions. No sign-up.
0 / 5Pick an answer to see why it is right or wrong.
SECTION 08Frequently asked questions
Do data scientists need Playwright?
Not usually as a core skill, but it is useful for testing model demo apps, capturing report screenshots and collecting permitted data from rendered pages.
Is web scraping allowed for data science projects?
It depends on the site's terms, the data and local law. Prefer official APIs or open datasets, avoid personal data and seek guidance when unsure.
Playwright or Selenium for data scientists?
Both work. Playwright offers built-in waiting and a convenient Python API; choose what your team uses.
Can Playwright test Streamlit or Gradio apps?
Yes. Any app that runs in a browser can be tested by loading its URL and interacting with its elements.
What Playwright project suits a data science portfolio?
A model demo app with end-to-end tests in CI and automated screenshots, with a README explaining responsible data use.
SECTION 09Related reads
Classroom & online · Noida
Build complete data science skills
Our Data Science Training Course covers Python, statistics, machine learning, evaluation and deployment basics, with projects that are tested and presentable.
Talk to a counsellor for current fees- Python and ML
- Model demos
- Portfolio projects
- Weekday & weekend batches