Playwright + Python · Lesson 22 of 37

0/37 done

Module 22: Windows, Tabs & Popups

22. Multi-Tab Handling & Browser Dialog Handlers

16 minlesson

Your task

Implement accept_all_dialogs(page):

  1. page.on("dialog", lambda d: d.accept())

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

Handling New Tabs via expect_page()

python
with page.expect_page() as new_tab_info:
    page.get_by_role("link", name="Privacy Policy").click()

new_tab = new_tab_info.value
new_tab.wait_for_load_state()
new_tab.close()

page.on("dialog", lambda dialog: dialog.accept())

Key Takeaways & Walkthrough Summary

Handling popups and external links that open in new tabs is seamless with Playwright. In this lesson, you will capture new pages with expect_page() and handle alerts using on_dialog listeners. Let's master multi-window automation!

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 are alerts automatically dismissed if no listener is registered?

Playwright auto-dismisses alerts by default so test suites never freeze waiting for user input on CI.

Ready? Solve the task in the editor

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

Python
Skip