Easy way to open new Tabs and Windows in Selenium4

New Feature in Selenium 4 – Easy way to open new Tabs and Windows

Welcome to 6th​​ article in Selenium4 new features series.​​ 

What you will Learn:

  • New Feature in Selenium 4 – Easy way to​​ open new Tabs

  • New Feature in Selenium 4 – Easy way to open new Windows

  • Code snippets

New Feature in Selenium 4 – Easy way to open new Tabs

In Selenium4, there is now an easy way to open new tabs and new windows. To quickly understand the difference between tab and window, let us launch​​ https://www.way2automation.com/ ​​​​ 

Click any of the 4 icons that you see on top right hand side (facebook, linkedin, youtube, etc..). Notice below that new tab opens up

Ctrl+N opens a new browser window as shown below​​ 

Let us now see how easy it is to open a new tab using selenium4.

Below are the steps of the use case that we would automate:

  • Launch​​ https://www.selenium-tutorial.com/courses​​ 

  • Store id of original window

  • Print id of original window

  • Create a new tab and switch to new tab

  • Store id of new tab

  • Print id of new tab

  • Execute the script, a blank new tab should open

The below piece of code would execute the​​ above steps. See line number 21 below. Selenium4 has a method​​ switchTo().newWindow(WindowType.TAB)​​ to provide an easy way to create a blank new tab and simultaneously switch to new tab as well​​ 

Rest of the steps are self-explanatory.

Run the script, notice below that a blank new tab opens

Below is the console output showing the 2 IDs that are different: ​​ 

Let us now extend our above use case by automating the below steps:

Below code will do the needful

Notice below that new tab now launches the new url

The console shows the page title

Next, let us continue automating below steps:

  • Close the new tab

  • Without switching to original window, let us try to print title of original window

Below code will perform above 2 additional steps:

Let us run the script. Notice that the new tab closes but we get NoSuchWindowException (see console o/p below). The reason we get this exception is because, selenium is still pointing to new tab. We have to tell selenium to switch to original window​​ 


Let us add the below step

Lets run the script now. Notice that there is no exception now and​​ selenium prints the title of original window

So this is how we can open a new tab in selenium4 and switch back to other tabs.

New Feature in Selenium 4 – Easy way to open new Windows

To open a new window, we can use​​ WindowType.WINDOW​​ as shown​​ below:

driver.switchTo().newWindow(WindowType.WINDOW);

Rest of the steps would be same.

If we execute the script, notice below that a new window opens and the console prints the respective outputs


Code snippet (new tab)

package​​ sel4scripts;

 

import​​ java.io.IOException;

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.WindowType;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ DemoNewTabs {

 

public​​ static​​ void​​ main(String[]​​ args)​​ throws​​ InterruptedException, IOException {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

driver.get("https://www.selenium-tutorial.com/courses");

//store id of original tab/window

 String​​ originalWindow​​ =​​ driver.getWindowHandle();

//print id of original tab/window

 System.out.println("Id of original tab: "​​ +​​ originalWindow);

//does 2 things: creates new tab &​​ switches to new tab

driver.switchTo().newWindow(WindowType.TAB);​​ 

//store id of new tab

 String​​ newWindow​​ =​​ driver.getWindowHandle(); ​​ 

//print id of new tab

 System.out.println("Id of new tab: "​​ +​​ newWindow); ​​ 

//on the new tab, navigate to​​ a different​​ url

driver.get("https://way2automation.com/");​​ 

//print title of new tab

 System.out.println("Title of new tab: "​​ +​​ driver.getTitle());​​ 

 Thread.sleep(2000);

 

//close the new tab

driver.close();​​ 

//Switch to default tab

driver.switchTo().window(originalWindow);​​ 

//print title of original window

 System.out.println("Title of original tab: "​​ +​​ driver.getTitle());​​ 

 }

 

}

 

Code snippet (new window)

package​​ sel4scripts;

 

import​​ java.io.IOException;

import​​ org.openqa.selenium.WebDriver;

import​​ org.openqa.selenium.WindowType;

import​​ org.openqa.selenium.chrome.ChromeDriver;

import​​ io.github.bonigarcia.wdm.WebDriverManager;

 

public​​ class​​ DemoNewWindow {

 

public​​ static​​ void​​ main(String[]​​ args)​​ throws​​ InterruptedException, IOException {

 WebDriverManager.chromedriver().setup();

 WebDriver​​ driver​​ =​​ new​​ ChromeDriver();

driver.get("https://www.selenium-tutorial.com/courses");

//store id of original window

 String​​ originalWindow​​ =​​ driver.getWindowHandle();

//print id of original window

 System.out.println("Id of original window: "​​ +​​ originalWindow);

//does 2 things: creates new window &​​ switches to new window

driver.switchTo().newWindow(WindowType.WINDOW);​​ 

//store id of new window

 String​​ newWindow​​ =​​ driver.getWindowHandle(); ​​ 

//print id of new window

 System.out.println("Id of new window: "​​ +​​ newWindow); ​​ 

//on the new​​ window, navigate to a different​​ url

driver.get("https://way2automation.com/");​​ 

//print title of new window

 System.out.println("Title of new window: "​​ +​​ driver.getTitle());​​ 

 Thread.sleep(2000);

 

//close the new window

//driver.close();​​ 

//Switch to original window

driver.switchTo().window(originalWindow);​​ 

//print title of original window

 System.out.println("Title of original window: "​​ +​​ driver.getTitle());​​ 

 }

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024