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.

Path progress
23%

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.

OverviewDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →

Try it

Run / study this snippet

with page.context.expect_page() as new_page_info:
Was this step clear?
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.