Playwright + Java · Lesson 23 of 38

0/38 done

Module 23: Windows, Tabs & Popups

23. Multi-Tab Navigation & Native Dialog Handling

16 minlesson

Your task

Implement acceptNextDialog:

  1. Accept Page page.
  2. Register page.onDialog(dialog -> dialog.accept()).

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

Handling New Tabs & Native Dialogs

java
Page newTab = page.waitForPage(() -> {
    page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Terms of Service")).click();
});

page.onDialog(dialog -> {
    dialog.accept();
});

Key Takeaways & Walkthrough Summary

Web apps frequently open external links in new browser tabs and prompt users with native alert dialogs. In this lesson, you will learn how to capture new tabs using waitForPage and handle browser dialogs with onDialog listeners. Let's master multi-window automation!

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

Common Doubts & Lesson Q&A

Why didn't my test see the browser alert?

Playwright automatically dismisses native alerts unless you register an explicit page.onDialog() listener before triggering the event.

Ready? Solve the task in the editor

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

Java
Skip