Playwright + Python · Lesson 15 of 37

0/37 done

Module 15: Browser, Context & Page

15. Browser, BrowserContext & Page Hierarchy

16 minlesson

Your task

Implement count_open_pages(context):

  1. Return len(context.pages) if context else 0.

Write it in the editor, then press Check answer (Ctrl + Enter).

Context Isolation Pattern

python
with sync_playwright() as p:
    browser = p.chromium.launch()

    admin_context = browser.new_context()
    admin_page = admin_context.new_page()
    admin_page.goto("https://targetqa.com/admin")

    customer_context = browser.new_context()
    customer_page = customer_context.new_page()
    customer_page.goto("https://targetqa.com/store")

    browser.close()

Key Takeaways & Walkthrough Summary

A single Browser instance can host dozens of isolated BrowserContexts. In this lesson, you will master context isolation, running multi-user scenarios with zero cookie leakage, and managing tabs. Let's master Contexts!

Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why use contexts instead of launching multiple browser instances?

Contexts spin up in ~15ms with virtually zero memory overhead, whereas launching a browser takes seconds and consumes high RAM.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip