Manuals / Playwright with Python / Chapter 15
Part 2 · Core Interactions · intermediate · ~40 min · Chapter 15 of 61
9. Tabs, Windows, iFrames
with page.context.expect_page() as new_page_info: page.get_by_role("link", name="Open in new tab").click() new_page = new_page_info.value new_page.wait_for_load_state() print(new_page.title()) The with ... expect_page() pattern registers the listener for the new page event before the click happens, avoiding a race condition where the new tab opens before you started listening for it.
Step 1 of 4
Overview
new_page = new_page_info.value The with ... expect_page() pattern registers the listener for the new page event before the click happens, avoiding a race condition where the new tab opens before you started listening for it.
Try it
Run / study this snippet
with page.context.expect_page() as new_page_info:Chapter learning outcomes
- with page.context.expect_page() as new_page_info: page.get_by_role("link", name=
- expect_page() pattern registers the listener for the new page event before the c
- page.context.
Clear these before you leave
Side quest
9. Tabs, Windows, iFrames deliverable
Apply one idea from “Overview” in a small script or note.