Handle Popup in Playwright Java

Profile picture for user arilio666
Submitted by arilio666 on

Popups are windows that suddenly appear in the foreground of the application while browsing or performing some action. These could be javascript dialog boxes, modal popups, advertisements, or browser window popups. Let's discuss how to handle different types of popups in Playwright Java.

Handle Browser Windows-based Popups using Popup Event.

To Practice, visit autopract to open the programsbuzz popup window after clicking the link Open Link in Popup.

This type of popup we will handle today is a windows-based popup.


    Page popUp = page.waitForPopup(() -> {

            page.locator("//a[normalize-space()='Open Link in Popup']").click();


So here, we used the waitForPopup method, and within its callback, we clicked the link for the popup.

Now we can get the title and other things using the variable popUp. Then we stored all those inside a page return type.

Handle Modal-Based Popup

Creating a new page instance to handle Modal Based Popups is unnecessary. You can use the same popup instance to handle the below popup, which will trigger after clicking the JQuery Popup Model button. The popup will display after 5 seconds of waiting.


        page.locator("//a[normalize-space()='JQuery Popup Model']").click();

        String textContent = page.locator("//p[normalize-space()='This is Sample Popup.']").textContent();