UI Automation: Selenium + Playwright + pytest · Lesson 6 of 6

The Hard Stuff: Dynamic Elements, iframes, Uploads & Network Stubbing

This is the last 20% of real apps — the tricky bits where beginners get stuck. Good news: four patterns handle almost all of it.

By Shahriyar · Updated

The idea, in one line

Late-appearing elements, iframes, file uploads, and fake network responses each have a clean, built-in answer in Playwright.

The four patterns

Advanced — faking the network with page.route

With page.route(pattern, handler) you intercept a request and answer it yourself using route.fulfill(json=...). Return a fixed catalog, force a 500 to test your error screen, or route.abort() to pretend the backend is dead.

See it work

▸ try it
from playwright.sync_api import Page, Route, expect

def test_error_banner_on_500(page: Page):
    # Force the products API to fail, then check the UI copes
    def fail(route: Route):
        route.fulfill(status=500, json={"error": "down"})
    page.route("**/api/v1/products", fail)

    page.goto("https://demo.playwright.dev/api-mocking")
    expect(page.get_by_text("Something went wrong")).to_be_visible()

def test_upload(page: Page):
    page.goto("https://the-internet.herokuapp.com/upload")
    page.locator("#file-upload").set_input_files("invoice.pdf")
    page.get_by_role("button", name="Upload").click()
    expect(page.get_by_text("File Uploaded!")).to_be_visible()

Read it top to bottom: the first test tells the products call to fail, then confirms the error message shows. The second uploads a file straight to the input and checks it succeeded.

Grounded in the official Playwright (Python) docs — Frames, File Uploads, Mock APIs

All lessons in UI Automation: Selenium + Playwright + pytest

  1. Selenium Locators: Finding the Element You Mean
  2. Waits, Actions & Frames — Killing Flakiness at the Source
  3. Playwright: Auto-Waiting, Locators & Tracing
  4. pytest Deep-Dive: Fixtures, conftest, Parametrize & Markers
  5. Page Object Model, Done Properly
  6. The Hard Stuff: Dynamic Elements, iframes, Uploads & Network Stubbing