Window Handling in Selenium- A Comprehensive Guide
Though developed as an internal tool in 2004, Selenium, with its latest version, is still used by most programmers and developers today. It is an open-source tool that automates tests to validate web applications across multiple browsers and operating systems through its various features. This blog will list all the information you need to know about Window Handling in Selenium.
Window in Selenium Overview
A window in a browser is the web page that the user encounters after clicking on a URL. It opens when the Selenium WebDriver session is created and has all the focus of WebDriver. The pop-ups within a web page are also considered as a window. Each window has a unique identifier, which remains the same throughout a single session. It is used to switch focus between windows when the user has to verify the contents of multiple windows. It becomes useful when the automated test tries to handle multiple windows. There are two types of windows in Selenium, as follows:
- Parent Window: The parent window is the main page where you perform any operations when you open a website. It is the first page that opens when you run the Selenium automation script.
- Child Window: Child windows are all the other windows that open inside the parent window. Keep in mind that there can be single or multiple child windows in a parent window.
For example, click on the following URL- https://internshala.com/ and the main or parent window of the website will open. Now in this window, when we click on the Register button, the registration window that opens will be called the child window.
Window Handling in Selenium
The window handling feature in Selenium is used for obtaining the handle of the window for the test to perform certain operations on it. For example, to click on a signup button on a certain web page, we first need to specify the address of the web page in the code. A window handle will do this since it is a unique identifier that stores the address of the browser windows. It is a pointer to a window that returns a String or alphanumeric value.
The window handling feature allows one to handle single as well as multiple windows in Selenium. Every window has a unique window handle value through which it is identified. Note that the WebDriver does not distinguish between windows and tabs. If your website has an open window or a tab, window handling works with both of them. WebDriver also cannot tell which window your operating system considers active. The switch method is used to handle windows in this case.
Methods of Window Handling in Selenium
The following methods are used to handle Windows in Selenium:
1. getWindowHandle()
With this method, we can get the window handle of the current window. It will return the value as a string type.
String MainWindow = driver.getWindowHandle();
System.out.printIn(“Main window handle is” + MainWindow);
The above code will give the outcome:
Main window handle is CDWindow-7BEFB11H273909A09
Here, the alphanumeric number is the unique window handle of the parent window.
2. getWindowHandles()
With this method, we can get the window handles of all the windows that are opened by the web driver. It will return the value as Set.
Set<String> allWindowHandles = driver.getWindowHandles();
System.out.printIn(“Child window handles are” +allWindowHandles);
The above code will give the outcome:
Child window handles are[CDWindow92H883920J39, CDWindow298u20H20290K302, CDWindow002387J3287982]
3. switchTo();
With this method, we can switch between various windows.
driver.switchTo().window(ChildWindow);
The above code will switch the current window to the window whose address is stored in ChildWindow in the code. For a better understanding, refer to the section dealing with how to handle multiple windows in Selenium.
Learn more about such Selenium WebDriver commands by going through this software testing course.
How to Handle a Single Child Window in Selenium?
The following are the steps to handle a single-child window in Selenium:
Step 1: First, import all the necessary classes for Selenium WebDriver, WebDriverWait, Desired Capabilities, etc. to set the respective browser capabilities and run the test. Ensure that you have installed the correct version of the browser driver.
Step 2: Then specify the browser driver to be used and its location. You can do so by using the following command:
System.setProperty(“webdriver.chrome.driver”, “D:chromedriver.exe”);
Step 3: Now instantiate the web driver on the new chrome driver using the command:
WebDriver driver = new ChromeDriver();
Step 4: Next, get the URL of the web page that you want to test using the driver.get(); command.
Step 5: We will now have to find the web element using the element locators. Refer to the following code for a better understanding:
WebElement clickElement = driver.findElement(By.id(“button”));
Step 6: Using the driver.getHandle(); method, store the handle of the parent window.
String MainWindow = driver.getWindowHandle();
System.out.printIn(“Main Window handle is” + MainWindow);
Step 7: Using the driver.getWindowHandles(); method, we have to now store the handles of all the opened child windows in a set of strings.
Set <String> a1 = driver.getWindowHandles();
System.out.printIn(“Child window handle is” + a1);
The above code will store the handles of all the windows in a set of strings.
Step 8: Switch to the child window after iterating through the set of window handles using the driver.SwitchTo() method.
Step 9: Now print the web page title and close it. (Refer to the following code to understand the syntax of steps 8 and 9 better.)
Iterator<String> b1 = a1.iterator();
while (b1.hasNext()) {
String ChildWindow = b1.next();
if (!MainWIndow.IqualsIgnoreCase(ChildWindow)) {
driver.switchTo().window(ChildWindow);
String pageTitle=driver.getTitle();
System.out.printIn(“Web page title of child window is-”+ pageTitle);
driver.close();
System.out.pirintIn(“The child window is closed.”);
}
}
Step 10: The final step is to just close the browser that was open using the closebrowser() method.
How to Handle Multiple Windows in Selenium?
Multiple window handling in Selenium has the same steps as single window handling. The only difference is that to handle multiple windows, you have to use conditional operators such as if-else in step 8 of the steps mentioned in the previous section. The following code to open windows for Facebook and Instagram sign-up will help you understand the if-else operators better.
Iterator<String> b1 = a1.iterator();
while (b1.hasNext()) {
String ChildWindow = b1.next();
if (!MainWindow.equalsIgnoreCase(ChildWindow)) {
driver.switchTo().window(ChildWindow);
String pageTitle=driver.getTitle();
System.out.println("The web page title of child window is:"+pageTitle);
if(pageTitle.contains("Facebook")){
WebElement signUpForFB= driver.findElement(By.xpath("//span[text()='Create New Account on Facebook']"));
signUpForFB.click();
System.out.println("Clicked on Login Option For Facebook");
}else if(pageTitle.contains("Instagram")){
WebElement signUpForIG= driver.findElement(By.xpath("//span[text()='Create account on Instagram']"));
signUpForIG.click();
System.out.println("Clicked on Follow Option For Instagram");
}
}
}
Any of the Selenium suite components can be used to write the above code. Since it is an open-source tool, you can easily download the suite components, such as SeleniumGrid, SeleniumIDE, or others, and begin working on them.
Closing All Windows in Selenium
To close the parent window at the end of a test, you have to switch to the window and then close it using the driver.close() method. Note that this will close the window that is being controlled at the moment.
But if you have to close all the opened windows at once, you can use the method driver.quit(). The method closes all the windows that are opened, regardless of the location of their control.
Conclusion
With this, we have come to the end of our blog. We have made learning how to handle Windows in Selenium easy by explaining it in detail. Once familiar with the syntax, it becomes easier to write code according to your specifications, and you are ready to ace a software testing career.