So far you have learned how to automate the objects of a web page after opening a URL. But what would happen if upon clicking on a button or a link it opens a new modal popup window, page in a new window, or a new tab. These are called the child window. By default, Selenium WebDriver does not identify the objects of the child window. If you simply try to automate the object of a child window you will get “NoSuchElementException“.In this tutorial, I will show you how to handle multiple windows in Selenium WebDriver.
Handle Multiple Windows in Selenium WebDriver
To demonstrate to you the scenario of handing multiple windows, we will open the W3School home page and click the button “Try It Yourself” to open a new window or child window and verify its heading.
In order to automate the above or such scenarios, we need to get the window handles of all child windows opened by Selenium WebDriver.
What is a Window Handle in Selenium WebDriver?
A Window Handle is a token that represents a resource that is managed by the Windows kernel. Anything that is open on a machine must have a window handle whether it is a browser, window application, or any file, etc. In simple words, you can consider it as a pointer to a window. Without a handle, Selenium can’t identify its child windows. The following screenshots demonstrate the same.
Selenium does have methods to retrieve window handles. We will use the following methods to automate the aforementioned scenario.
- get.windowhandle(): It returns the window handle of the current window
- get.windowhandles(): It returns the window handles of all the windows opened by Selenium WebDriver
- set: The set method is used to set the window handles in the form of a string. set<string> set= driver.get.windowhandles()
- switch to: It is used to switch between the open windows
Example – Handling Multiple Windows in Selenium WebDriver
Test Scenario:
- Open W3School home page
- Store the parent window title in a variable using command String parent=driver.getWindowHandle();
- Click on the link “Try It Yourself”.It will open a New Tab (Child window). So now we will have two windows.
- Store the window handles of all windows using command Set<String> allWindowHandles = driver.getWindowHandles();
- Iterate through all window handles to find child window handles
- Print all Child window titles
- Verify the Title of the child windows.
- If the child window title matches the desired title, get its heading
- Print the retrieved heading in step 8
- Switch back to the parent window using the parent window handle
- Retrieve the heading of the parent window
- Print the heading retrieved at step 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ExampleofHandlingMultipleWindowInSelenium { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; //Setting System property for Chrome browser Driver. System.setProperty("webdriver.chrome.driver",BrowserDriverPath); //Create a new Instance for Chrome Browser WebDriver driver = new ChromeDriver(); //Defining Implicit Wait for 20 Seconds driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://www.w3schools.com/"); //Storing the Parent window handle to switch back to parent window whenever required String parent=driver.getWindowHandle(); //Clicking on "Try It Yourself" button driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[2]/div/a")).click(); //When multiple child windows are open getting their window handles in a Set Set<String> winHandleSet = driver.getWindowHandles(); // Now iterating all window handles Iterator<String> iterator= winHandleSet.iterator(); while(iterator.hasNext()) { String childWindow=iterator.next(); System.out.println("Current Window Handle Id is: "+childWindow); //if parent window handle id is not equal to Child window handle id if(!parent.equals(childWindow)) { //Switching to the child window driver.switchTo().window(childWindow); System.out.println("Child Window Title is: "+driver.switchTo().window(childWindow).getTitle()); if(driver.switchTo().window(childWindow).getTitle().equalsIgnoreCase("Tryit Editor v3.6")) { //In this example the required objects are inside in an iframe of child window so it is required witch into the iframe // so that Selenium can identifies the objects that are inside iframe of the DOM driver.switchTo().frame("iframeResult"); //Getting the Heading of Child window String childPageHeading=driver.findElement(By.xpath("/html/body/h1")).getText(); System.out.println("Child Page Heading: "+ childPageHeading); } //Uncomment the following code to close the child window only //driver.close(); } } //switch to the parent window driver.switchTo().window(parent); String parentWindowHeading = driver.findElement(By.xpath("//*[@id=\"main\"]/div[1]/div[1]/h1")).getText(); System.out.println("Parent Page Heading: "+ parentWindowHeading); //Uncomment the following code to close all browsers opened by Selenium WebDriver //driver.quit(); } } |
Output
Current Window Id is: CDwindow-5A489E5801B98C7E5F053EE9C90AB4C3
Current Window Id is: CDwindow-1B05460146A811FE074C556CE269584F
New Child Window Title: Tryit Editor v3.6
Child Page Heading: This is a Heading
Parent Page Heading: HTML
Conclusion
We can handle multiple windows in Selenium WebDriver.We can use getWindowHandle() and getWindowHandles() methods respectively to get the window handle of the current window as well as all windows opened by Selenium. We can switch to any specific child window by using its window handle with the help of the command driver.switchTo().window(WindowHandle);
Recommended Posts
- Handle Dynamic Web Tables In Selenium WebDriver
- Wait Commands in Selenium WebDriver | Implicit | Explicit | Fluent Wait
- Top #23 Most Useful Selenium WebDriver Commands That You Must know
- XPath in Selenium WebDriver with Example
- How to Use CSS Selector in Selenium WebDriver | 13 Examples
- How to Use AutoIT In Selenium WebDriver
- All about 8 Locators in Selenium Webdriver with Examples