Blog

Playwright Python Cookbook: Playwright Python vs Selenium and Browser Automation Alternatives

Pick Playwright Python for most new browser automation work. It is faster to set up, easier to read, and less picky than Selenium in many modern web apps. Selenium is still useful, especially for old test suites and huge browser grids. But if you are starting fresh, Playwright feels like the calmer tool.

TLDR: Playwright Python is a great choice for scraping, testing, and browser tasks that need speed and clean code. For example, a QA team running 500 login tests may cut flaky failures from 12% to 3% by using Playwright’s auto waiting and browser contexts. Selenium still wins when you need deep legacy support or an existing Selenium Grid. Tools like Puppeteer, Cypress, and Requests also fit special cases.

Why Playwright Python feels so good

Playwright was built for the modern web. That means single page apps. Popups. Tabs. Downloads. Shadow DOM. Mobile views. All the stuff that makes older tools sigh loudly.

With Playwright Python, you can control Chromium, Firefox, and WebKit. WebKit matters because it helps you test Safari-like behavior without owning a drawer full of Apple devices.

Here is a tiny example:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto("https://example.com")
    print(page.title())
    browser.close()

That is clean. No driver download party. No “which ChromeDriver version matches Chrome today?” guessing game. Honestly, that part alone saves sanity.

Playwright Python vs Selenium

Selenium is the old champ. It has been around for years. It works with many languages. It has a giant user base. Many companies already have Selenium tests, reports, grids, and CI jobs.

But Selenium can feel heavy. You often manage waits by hand. You may fight stale elements. You may also need browser drivers. It is not always painful, but when it is painful, it is very painful.

Playwright handles waiting in a smarter way. It waits for elements to be ready before clicking or typing. This reduces random failures.

Feature Playwright Python Selenium
Setup Simple install and browser setup May need matching drivers
Speed Usually fast Good, but often slower
Auto waiting Built in and smart Often manual
Browser support Chromium, Firefox, WebKit Very broad
Legacy use Newer tool Great for older systems

A simple cookbook setup

Install Playwright like this:

pip install playwright
playwright install

That second command installs the browsers. Nice and tidy.

Now try a login flow:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com/login")
    page.fill("#email", "chef@example.com")
    page.fill("#password", "secret")
    page.click("button[type='submit']")
    page.wait_for_url("/dashboard")
    browser.close()

This reads like a recipe. Open page. Fill fields. Click button. Wait for dashboard. Done.

The catch is that real sites love to misbehave. Banners slide in. Cookies pop up. Buttons move. A frontend release changes one class name and your test falls over like a cheap chair. Playwright helps, but it cannot fix messy app design.

Browser contexts are the secret sauce

Playwright has browser contexts. Think of them as fresh browser profiles. Each context has its own cookies, storage, and session.

This is useful for testing users at the same time.

  • Admin user creates a product.
  • Buyer user sees the product.
  • Guest user cannot access the admin page.

All of this can happen in one test. No messy logout loops. No cookie cleanup drama.

When Selenium is still the better pick

Do not throw Selenium into the sea. It still has strong use cases.

  • You already have thousands of Selenium tests.
  • Your team knows Selenium very well.
  • You use a mature Selenium Grid.
  • You need a rare browser or device setup.
  • Your company has strict tooling rules.

If a test suite has 10,000 Selenium tests, migrating just because Playwright is shiny may be silly. Measure first. Move high-value tests first. Start with flaky tests. Those give the fastest win.

What about Puppeteer?

Puppeteer is great. It is close to Chrome and works nicely with JavaScript. Many scraping scripts use it. Many PDF generators use it too.

But Puppeteer is mainly tied to the Chrome family. Playwright gives you Chromium, Firefox, and WebKit from one API. If Python is your main language, Playwright Python is also a better fit.

Use Puppeteer if your stack is Node.js and Chrome-only work is fine. Use Playwright Python if you want broader browser checks and Python-friendly code.

What about Cypress?

Cypress is loved by frontend teams. Its test runner is friendly. Its visual debugging is slick. It makes testing feel less scary.

But Cypress is not the best general browser robot. It runs in a special way inside the browser. That can be great for app testing, but limiting for multi-tab flows and other browser-level tasks.

Pick Cypress for frontend component and end-to-end tests in JavaScript projects. Pick Playwright when you need more control over the browser itself.

What about Requests and Beautiful Soup?

Sometimes you do not need a browser at all. This is where people waste time. They open a full browser to fetch plain HTML. Please do not do that.

If the site sends all useful data in the first HTML response, use Requests and Beautiful Soup. They are faster and lighter.

Use a browser only when you need:

  • JavaScript rendering.
  • Login sessions.
  • Button clicks.
  • Scrolling content.
  • CAPTCHA handling checks.
  • Download flows.

A Requests script may finish in 0.4 seconds. A browser task may take 4 seconds. That adds up. Run 10,000 pages and you will feel the pain.

Scraping with Playwright Python

Playwright is useful for scraping JavaScript-heavy pages. You can wait for cards, click “load more,” and catch network calls.

items = page.locator(".product-card")
print(items.count())

for i in range(items.count()):
    print(items.nth(i).inner_text())

Still, scrape politely. Respect robots rules where needed. Add delays. Do not hammer sites. Do not collect private data. A fast tool is not a permission slip.

Testing with Playwright Python

For testing, Playwright shines because it reduces waiting bugs. It can take screenshots. It can record videos. It can trace a failed test. That trace viewer is gold.

When a test fails, you can see what happened step by step. The page loaded. The click happened. The modal appeared. The selector failed. No more guessing from a sad red log.

Basic test tools pair well with it:

  • pytest for test structure.
  • pytest-playwright for fixtures.
  • GitHub Actions for CI runs.
  • Allure or HTML reports for results.

Best cookbook tips

  • Use role selectors. They are closer to how users see the page.
  • Avoid brittle CSS paths. Long selectors break fast.
  • Save auth state. Skip repeated logins when possible.
  • Run headless in CI. Use headed mode for debugging.
  • Turn on traces for retries. Debug flakes faster.
  • Mock APIs when testing edge cases. It is faster than waiting for a real backend to act weird.

So, what should you choose?

Choose Playwright Python for new Python automation, modern web apps, scraping with JavaScript, and reliable end-to-end tests. Choose Selenium when your team has a large current setup or needs rare browser support. Choose Requests when a browser is overkill.

The simple rule is this: use the smallest tool that gets the job done. If plain HTTP works, skip the browser. If you need a real browser, Playwright Python is the sweet spot. It is fast, clean, and far less annoying than many older setups.