Google News
logo
Selenium - Interview Questions
Better Window/Tab Management in Selenium 4
There are several instances in test automation wherein one might need to open a particular link in a new tab or window to perform certain actions. To achieve this in Selenium 3, QAs had to create a new driver object and then perform the switch operation using the WindowHandle method to perform subsequent steps.
 
This is set to change in Selenium 4 as it comes with a new API – newWindow that allows users to create and switch to a new window/tab without creating a new WebDriver object.
 
Sample code snippet to open a new window
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);

// Opens Free Time Learn homepage in the newly opened window
driver.navigate().to("https://www.freetimelearning.com/");

Sample code snippet to open a new tab within the same window
driver.get("https://www.google.com/");

// Opens a new tab in existing window
driver.switchTo().newWindow(WindowType.TAB);

// Opens Free Time Learn homepage in the newly opened tab
driver.navigate().to("https://www.freetimelearning.com/");
Advertisement