Handling windows and tabs is a crucial aspect of browser automation, and Playwright Java provides powerful capabilities to manage them effectively. This article will discuss about handling windows and tabs using Playwright Java.
- Open a new window using Playwright
- Switch to a new window
- Open a new tab using Playwright
- Switch to a new tab
Let’s look at them one by one.
Open a new window using Playwright
In Playwright, a browser context typically corresponds to a window. Therefore, utilizing a browser context allows for managing multiple windows.
Making an instance of a browser
1 2 | Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); |
Create a new browser context which represents a window
1 2 | // creating a BrowserContext BrowserContext browserContext = browser.newContext(); |
Creating a Page and navigating to the URL
1 2 3 4 | Page page = browserContext.newPage(); // Navigating to the URL |
To open a new window, create a new browser context.
1 2 | // creating a new BrowserContext BrowserContext browserContext2 = browser.newContext(); |
We can now navigate to any URL
1 2 | Page page2 = browserContext2.newPage(); |
Here is the whole code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserContext; import com.microsoft.playwright.Page; import com.microsoft.playwright.Playwright; public class CodekruTest { public static void main(String[] args) { Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); // creating a BrowserContext BrowserContext browserContext = browser.newContext(); Page page = browserContext.newPage(); // Navigating to the URL System.out.println( "Title of current window: " + page.title()); // Creating a new BrowserContext BrowserContext browserContext2 = browser.newContext(); // Navigating to the new URL Page page2 = browserContext2.newPage(); System.out.println( "Title of new window: " + page2.title()); // closing the instances browser.close(); playwright.close(); } } |
Output –
Title of current window: Text Fields
Title of new window: Buttons
Switch to a new window
As previously mentioned, Playwright utilizes browser context to efficiently handle multiple windows. This allows for seamless switching between windows and executing various actions.
Get all browser contexts
1 2 | // get all browser context List<BrowserContext> contexts = browser.contexts(); |
Iterate through the list and perform actions on your desired window
1 2 3 4 5 6 7 | for (BrowserContext context : contexts) { // get first page for a browser context Page currentPage = context.pages().get( 0 ); System.out.println( "Title of Page: " + currentPage.title()); } |
Below is the whole code for the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.List; import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserContext; import com.microsoft.playwright.Page; import com.microsoft.playwright.Playwright; public class CodekruTest { public static void main(String[] args) { Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); // creating a BrowserContext BrowserContext browserContext = browser.newContext(); Page page = browserContext.newPage(); // Navigating to the URL // Creating a new BrowserContext BrowserContext browserContext2 = browser.newContext(); // Navigating to the new URL Page page2 = browserContext2.newPage(); List<BrowserContext> contexts = browser.contexts(); for (BrowserContext context : contexts) { // get first page for a browser context Page currentPage = context.pages().get( 0 ); System.out.println( "Title of Page: " + currentPage.title()); } // closing the instances browser.close(); playwright.close(); } } |
Output –
Title of Page: Text Fields
Title of Page: Buttons
Open a new tab using Playwright
In Playwright, a Page typically refers to a tab within a browser window. It’s helpful to think of browserContext as representing the entire browser window, with each Page representing a single tab within that window. Similar to how a window can have multiple tabs, a browserContext can have multiple Pages.
Creating Browser context
1 2 3 4 5 | Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); // creating a BrowserContext BrowserContext browserContext = browser.newContext(); |
Opening the first tab
1 2 3 4 5 | // creating a page Page page1 = browserContext.newPage(); // Navigating to the URL |
Opening the second tab
1 2 3 | // Opening second tab Page page2 = browserContext.newPage(); |
Here is the whole code where we have opened two tabs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserContext; import com.microsoft.playwright.Page; import com.microsoft.playwright.Playwright; public class CodekruTest { public static void main(String[] args) { Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); // creating a BrowserContext BrowserContext browserContext = browser.newContext(); // creating a page Page page1 = browserContext.newPage(); // Navigating to the URL System.out.println( "Current tab title: " + page1.title()); // Opening second tab Page page2 = browserContext.newPage(); System.out.println( "New tab title: " + page2.title()); // closing the instances browser.close(); playwright.close(); } } |
Output –
Current tab title: Text Fields
New tab title: Buttons
Switch to a new tab
Managing multiple tabs within a single browser window can be a challenge. Playwright provides us with the ability to iterate through these tabs and execute various actions on the specific tab we desire.
Get all tabs of a browser window
We can use the pages() method of BrowserContext to get all pages ( or we can say tabs ) of a browser window.
1 2 | // get all pages List<Page> pages = browserContext.pages(); |
Switching between tabs
Now, we can switch between the tabs and perform various actions on the desired tabs.
1 2 3 4 | // iterate over pages for (Page singlePage: pages) { System.out.println( "Title of the page: " + singlePage.title()); } |
Below is the whole code –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import java.util.List; import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserContext; import com.microsoft.playwright.Page; import com.microsoft.playwright.Playwright; public class CodekruTest { public static void main(String[] args) { Playwright playwright = Playwright.create(); Browser browser = playwright.chromium().launch(); // creating a BrowserContext BrowserContext browserContext = browser.newContext(); // creating a page Page page1 = browserContext.newPage(); // Navigating to the URL // Opening second tab Page page2 = browserContext.newPage(); // get all pages List<Page> pages = browserContext.pages(); // iterate over pages for (Page singlePage: pages) { System.out.println( "Title of the page: " + singlePage.title()); } // closing the instances browser.close(); playwright.close(); } } |
Output –
Title of the page: Text Fields
Title of the page: Buttons
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.