testsigma
How to Handle Multiple Tabs and Windows Using Selenium

How to Handle Multiple Tabs and Windows Using Selenium

Since Selenium is open source, it is widely used throughout the world. No matter how complicated the user interface (UI) is, Selenium offers several methods to automate it, decreasing or eliminating the need for manual work. 

Webdriver does not differentiate between windows and tabs. If you open any new tab or window, both can be handled using window handles. Each window or tab has a unique identifier which remains the same for a session.

This blog post will guide you on using a Selenium script to manage multiple browser tabs. We will see possible ways to handle various tabs or windows for this task. 

How to Open a New Tab in Selenium with Java?

Opening a new tab has always been challenging work. With Selenium, there are different ways by which we can open a new tab.

  1. With the help of the Action class
  2. With the help of the Robot framework 
  3. With the help of Selenium 4
  4. With the help of JavascriptExecutor

1. With the help of the Action class:

In Selenium, the Action class simulates complex user interactions such as mouse movements, key presses, and context menu interactions. It allows you to perform multiple actions in a single call and specify the order in which the actions are performed. You can use the Action class to perform actions such as moving the mouse to a specific element, clicking, and holding a mouse button, releasing a mouse button, and performing a keypress. The Action class is typically used in conjunction with the WebDriver class to perform actions on web elements.

Let’s see how we can open a new tab using the Action class in Selenium.

2. With the help of the Robot framework:

Robot Framework has a rich ecosystem of libraries and tools that can be used for test automation. These libraries provide keywords for everyday tasks such as web testing, database testing, and API testing. Additionally, it also can create custom libraries using Python or Java.

Let’s understand how to open a new tab using the robot framework.

The above code will help you open a new tab; it works similarly to pressing ‘control + t’ on your keyboard manually.

3. With the help of Selenium 4:

With the newWindow API in Selenium 4, you can easily create new windows (or tabs) and have Selenium 4 switch to them for you. It avoids creating a new WebDriver object because the new window or tab is created within the same session.

Let’s understand how to open a new tab using Selenium 4.0

4. With the help of JavascriptExecutor:

You can also open a new tab with the help of JavaScriptExecutor. You can use the executeScript() method of the JavascriptExecutor interface in Selenium to open a new tab using JavaScript. Let’s understand by an example.

If you want to open a blank new tab

If you want to launch a website in a new tab

Before we move ahead, let’s understand what getWindowHandle() and getWindowHandles() methods are.

1. getWindowHandle()

getWindowHandle() is a method provided by the WebDriver interface in Selenium. It returns the current window handle as a string. This handle can switch to that specific window or tab using the switchTo().window() method.

Here’s an example of how you can use getWindowHandle()

In this example, currentHandle will be set to a string that represents the handle of the current window or tab 

2. getWindowHandles()

getWindowHandles() is a method provided by the WebDriver interface in Selenium. It returns a set of window handles, each representing a unique window or tab. This method can be used to switch between multiple windows or tabs open in the browser.

Here’s an example of how you can use getWindowHandles()

In this example, handles will be set to a set of strings, each representing a unique window or tab.

You can use a for-each loop to iterate through the set of handles and use switchTo().window(handle) method to switch to the desired window.

How to Handle Multiple Tabs in Selenium Using JavascriptExecutor?

In Selenium, you can use the JavascriptExecutor interface to handle multiple tabs. To switch to a different tab, you can use the executeScript method to execute JavaScript, which will change the focus to the desired tab.

Here’s an example of how to switch to a different tab using the JavascriptExecutor:

Code snippet:

How to Handle Multiple Tabs in Selenium Using the Robot Framework?

In Selenium, using the Robot Framework, you can handle multiple tabs using the Switch Window keyword. This keyword allows you to switch between different windows or tabs open in the browser.

The syntax to open a new tab is listed below. 

Code snippet:

How to Close a Tab in Selenium?

In Selenium webdriver, there are two by which you can close your tab or window, close() and quit() methods on the WebDriver. For example, if your WebDriver object is named driver, you can close the current tab with the following command.

Close()

The focus-right browser window is closed using the Webdriver command’s close() method. When several browser tabs or windows are open, it is preferable to use the close() command. The close()command will end the current browser session if only one window is active in the entire browser.

quit()

The quit() command ends the current browser session, including all of its windows and tabs. When a user wants to end a program, they use this command. The WebDriver session won’t completely close if you don’t call quit() at the end of the program, which could cause memory leaks because files won’t be removed from memory.

Let’s understand by the below example. 

Code snippet:

How to Switch to the Default Tab in Selenium? 

In Selenium with Java, you can switch to the default tab using the switchTo().window(currentHandle) method. This method will switch the focus of the web driver back to the original window.

Below is the syntax:

Code snippet

Conclusion

The above blog demonstrated how we could handle multiple tabs or windows with Selenium webdriver using Action class, JavascriptExecutor, or Robot framework. One of the complex situations to handle is multiple tabs or windows and performing actions on that focussed window or tab. We have seen sample code for handling  single or multiple windows.

Do share your thoughts on how you have handled the windows or tabs in your scripts.

Happy testing…!!!

Frequently Asked Questions

Can Selenium work on multiple tabs?

Yes, Selenium can work on multiple tabs. The Selenium WebDriver API has methods to switch between different tabs or windows in a browser, such as the switch_to.window() method allows you to switch to a specific window or tab by its handle.

Can we handle multiple windows in Selenium?

Yes, Selenium can work on multiple windows. getWindowHandles() is a method provided by the WebDriver interface in Selenium. It returns a set of window handles, each representing a unique window or tab. This method can be used to switch between multiple windows or tabs open in the browser.


Test automation made easy

Start your smart continuous testing journey today with Testsigma.

SHARE THIS BLOG

RELATED POSTS


Power of POC in Testing: Your Exclusive Guide to Success
Power of POC in Testing: Your Exclusive Guide to Success
performance testing tools_banner image
Test objects in software testing | Types & How to Create it?
How To Write Calculator Test Cases With Sample Test Cases
How To Write Calculator Test Cases? With Sample Test Cases