Playwright

Multi-tab Browser Automation

Thirdy Gayares
12 min read

🎯 What You Will Learn

Open, list, select, and close tabs while keeping page references and workflow state predictable.

  • Create and identify browser tabs
  • Switch tabs without reusing stale references
  • Coordinate a two-page research workflow
  • Close tabs and sessions cleanly
Prerequisites: Playwright CLI configured as PWCLI and familiarity with snapshots.

Treat Every Tab as a Separate Page

Each tab has its own URL, DOM, history, and refs. A session can own several tabs, but only one is active for CLI actions at a time.

CommandEffect
tab-new [url]Create a new page and optionally navigate it
tab-listShow tab indexes and URLs
tab-select <index>Make one page active
tab-close [index]Close the active or specified page

Open the Primary Tab

Start the session on the source page and inspect it before creating more tabs.

Terminal
"$PWCLI" -s=research open https://playwright.dev --headed
"$PWCLI" -s=research snapshot
"$PWCLI" -s=research tab-list

Create a Second Tab

Open the locator documentation in another tab, then list tabs so you know the assigned index.

Terminal
"$PWCLI" -s=research tab-new https://playwright.dev/docs/locators
"$PWCLI" -s=research tab-list

Select and Snapshot

Tab selection changes the active Page. Always snapshot after selection before using any ref.

Terminal
"$PWCLI" -s=research tab-select 1
"$PWCLI" -s=research snapshot
"$PWCLI" -s=research find "Locate by role"

Move Between Tabs Deliberately

List before selecting when a popup may have changed tab order. Treat the returned index as current session state rather than a permanent value.

Terminal
"$PWCLI" -s=research tab-list
"$PWCLI" -s=research tab-select 0
"$PWCLI" -s=research snapshot

A click may open a popup instead of navigating the active page. Compare tab-list output before and after the click to find the new tab.

  1. List tabs and note the indexes.
  2. Snapshot and click the popup-producing link.
  3. List tabs again.
  4. Select the new index and snapshot it.

Avoid Cross-tab References

References are meaningful only for the page snapshot that produced them. Switching back to a tab does not make a ref from another page valid.

Cross-tab bug: If a command reports a missing target after a tab switch, snapshot the active tab instead of trying random refs.

Close Tabs and the Session

Close temporary tabs when the workflow finishes, verify the remaining list, then close the browser session.

Terminal
"$PWCLI" -s=research tab-close 1
"$PWCLI" -s=research tab-list
"$PWCLI" -s=research close

Recap

  • Every tab is a separate Page.
  • List tabs before relying on an index.
  • Select, snapshot, then interact.
  • Never carry element refs across tabs.

Checkpoint: You have completed the workflow and have a repeatable reference for your next Playwright project.

Official Resources

About the Author

TG

Thirdy Gayares

Passionate developer creating custom solutions for everyone. I specialize in building user-friendly tools that solve real-world problems while maintaining the highest standards of security and privacy.