How to Handle a new Tab in Selenium WebDriver & Switch Between Tabs

In this cutting-edge technology era where innovation is progressing at a huge speed, the complexity in the design of the applications is likewise being more complexed day by day, and automating them additionally turns out to be a very challengeable task.

How to Handle a New Tab in Selenium WebDriver

Such a complex scenario is automating popups or pages opened in new tabs. You may have to switch in between multiple tabs open in a browser to perform some actions on them. If you are a beginner in Selenium you will get stuck and feel a bit annoying. Having said that, the good thing is that its a quite an easier thing.

This article will describe to you how to handle a new tab in Selenium Webdriver and perform all sorts of activities that will sort out all of your problems related to a new tab.

Handle a New Tab in Selenium WebDriver

You will learn the following things.

  • Open a link in a New Tab in Selenium WebDriver and perform some action on it.
  • Open a link in a New Tab in Selenium WebDriver Using JavaScript and perform some action on it.
  • Open New Tab in Selenium WebDriver Action Class and perform some action on it.
  • Switching back and forth between parent and new tab

We will use windows handle to handle new tabs. If you want to know more about window handles please refer to my post How to Handle Multiple Windows In Selenium WebDriver.

How to Open a Link in a New Tab in Selenium WebDriver and Perform some action on it

You can simply open a link in a new tab by following the syntax

The following code shows how to print the title and the page heading after opening a link on a new tab.

Output

New Tab Window Title: CSS Tutorial
New Tab Window Page Heading: CSS Tutorial

How to Open a Link in a New Tab in Selenium WebDriver Using JavaScript and Perform some action on it

You can use the following syntax to open a link in a new tab using JavaScript.

((JavascriptExecutor)driver).executeScript(“window.open(‘ ” + linkVaue +” ‘);”);

The following code shows how to use the above syntax.

How to Open a Link in a New Tab in Selenium WebDriver Using Action Class and Perform some action on it

You can use the following syntax to open a link in a new tab using the Action class.

The following code shows how to use the above syntax.

Switching Between Parent and New Tab

The following example will open a new tab and switch to the new tab. It will retrieve the title and heading of the new tab window and print them. After that, it will switch back to the parent window and print its title.

Note: If you have to deal with more than two tabs and switch in between any of the tabs it’s better to get the window handles in a set and iterate them to retrieve their window title.If the expected title is matched then switch to that window. Please refer to the post How to Handle Multiple Windows In Selenium WebDriver to see the example.

Open Multiple Tabs and Navigate to Different URLs On each of Them and Iterate Them

So far we have seen how to open a link in a new tab and performed some action on it. You may have a requirement that after opening a page you may have to open one or more than one new tab and then open different URLs on them and do some automation testing on them. I am going to show you how to do it right away.

We can open new empty tabs in the following two ways:

  1. Using Robot Class
  2. Using JavaScript

Syntax of the opening new tab using Robot class

The above code opens a tab using the simulating keypress event  “CTRL+ t” command of the keyboard. The same thing can be done using sendKeys but it may or may not work depending on the browser you are using. You can use the following sendKeys command to open a new tab.

driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL+ “t”);

Syntax of the opening new tab using JavaScript

The following is the syntax to open a new tab using JavaScript

((JavascriptExecutor)driver).executeScript(“window.open()”);

We will use both the above commands to open new tabs in the following test scenario.

Test Scenario

  • Open a browser and navigate to W3school 
  • Open a new tab using Robot class
  • Open a new tab using javascript
  • Open Google at tab index 1
  • Open Facebook at tab index 2
  • Iterate the tabs and print their title

Example of Opening Multiple Tabs and Navigating to Different URLs on each of them

OutPut

Title of Tab of at Index (0) is: W3Schools Online Web Tutorials
Title of Tab of at Index (1) is: Google
Title of Tab of at Index (2) is: Facebook – log in or sign up

Conclusion

We have seen that there are different ways to open a new tab. You can use any of them that suits you better. If you have any comment or query please mention in the comment box.

Recommended Posts

Leave a Reply