How to Handle Multiple windows or tabs using selenium 4.0

Reading Time: 3 minutes

Hello Everyone, in this blog we will learn how we can handle multiple Windows or tabs using selenium 4.0.

Scenario for Handle Multiple Tabs or Windows

Suppose we are on the website www.knoldus.com/home, when we enter this website in our selenium script it directs us to the Knoldus homepage, and at that time if we want to open a new page in a New Tab then we simply right click on Button and click on “Open in new tab” or we can press CONTROL from the keyboard and click on the button for the new tab.

In our selenium script, we will now change the control of the parent tab i.e Knoldus Homepage to the child tab. So the thing is we will change the control from one window to another window, or we can say that we will handle multiple windows or tabs in selenium.

Let’s start with the script

package windows;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class NewTab {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "src/test/chromedriver_linux64/chromedriver");

        // Launch Chrome
        WebDriver driver = new ChromeDriver(); 

        // Maximize Window
        driver.manage().window().maximize();

        // Delete all Cookies
        driver.manage().deleteAllCookies(); 

        // Waits
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get("https://www.knoldus.com/home");
        String s = Keys.chord(Keys.CONTROL, Keys.ENTER);
        driver.findElement(By.xpath("//a[@class='btn-banner shift-up py-2 py-lg-4 px-3 px-lg-4 position-relative d-table']")).sendKeys(s);
        Set<String> windows = driver.getWindowHandles();
        Iterator<String> it = windows.iterator();
        String parent = it.next();
        String child = it.next();
        driver.switchTo().window(child);
        driver.findElement(By.xpath("//p[@class='mb-0']")).click();
        driver.switchTo().window(parent);
        driver.quit();
    }
}
  • Firstly, we have to invoke our chrome browser and import all the packages required.
// Launch Chrome
WebDriver driver = new ChromeDriver();
  • Let’s hit the url
 driver.get("https://www.knoldus.com/home");
  • Now we have to open our case studies page in new tab
  • Let’s inspect ‘Check our work, our passion!’ Button and verify its xpath for locate this element. In short, use any locator to locate the element. For this we will use driver.findElement() method. Usually we use ”’.click()”’ after this method but we have to open this in new tab so we have to tell selenium that hold the ‘CONTROL’ key from the keyboard.
String s = Keys.chord(Keys.CONTROL, Keys.ENTER); driver.findElement(By.xpath("//a[@class='btn-banner shift-up py-2 py-lg-4 px-3 px-lg-4 position-relative d-table']")).sendKeys(s);

Keys.chord() is a method in selenium that is used to trigger multiple keys simultaneously. We told selenium to hold the CONTROL key and then press enter in the above code. This will open the case studies page link of the knoldus in a new tab.

  • Now we are having two tabs. Therefore, we have to give the control to each and every tab. For that we have special method called ‘getWindowHandles()‘, we have to return the data in string form and import the package for the same.
  • After that, we have to iterate through each and every tab. We have one class in selenium called ‘Iterator’ and we have to return it in string.
Set<String> windows = driver.getWindowHandles();
Iterator <String> it = windows.iterator();
  • After that, we have to switch between parent and child tabs and for this we will use method called next(). This method will move the handle to the next tab that is available and switch the tab.
String parent = it.next();
String child = it.next();
driver.switchTo().window(child);
  • Now we will verify whether control of driver is at child tab or not. For this, we will click on ‘Schedule a meeting’ button through button’s xpath.
driver.findElement(By.xpath("//p[@class='mb-0']")).click();
  • Again, switch to parent tab and quit both the tabs by driver.quit() method –
driver.switchTo().window(parent);
driver.quit();

For closing all the tabs in selenium we have to use the driver.quit() method. Because it will close all the instances opened by a google chrome browser. But the driver.close() method will close only a single tab.

In this example, we handle switching over multiple tabs. Using Selenium 4.0, we can also handle Windows in the same way as we do tabs.

I hope this article will be helpful for you, Thank you!

Reference

https://www.selenium.dev/documentation/legacy/selenium_ide/#alerts-popups-and-multiple-windows


Knoldus-blog-footer-image

Written by 

I am Software Consultant - QA at Knoldus Inc. I have completed my B.Tech from Jodhpur Institute of Engineering and Technology.