How To Handle Multiple Windows in Selenium WebDriver?

  • Handling Multiple Windows in WebDriver

Handling multiple windows in Selenium WebDriver has always been a little tricky and required an extra effort.

Before commencing, let us first consider a few situations when we are likely to deal with multiple windows in selenium

  • Filling forms may require to select the date from a separately opened window.
  • Clicking on some link/button can kick-off yet another window.
  • Handling Advertisement windows

Hence, we can come up with various scenarios depending upon the application.

Now let us motion ourselves towards the challenge we face under above situations. The most particular of all is switching the focus from one window to another. Let us understand the same in the following way:

Handling Multiple Windows in WebDriver

Comprehending from the above figure, the entire process can be fundamentally segregated into following steps:

Step 1 : Clicking on Link1 on Window A

A new Window B is opened.

Step 2 : Move Focus from Window A to Window B

Window B is active now

Step 3: Perform Actions on Window B

Complete the entire set of Actions

Step 4: Move Focus from Window B to Window A

Window A is active now

These are the steps which we can easily interpret out of the diagram, but there are a few more steps to add to complete this process and making our script execute. These steps don’t have visibility but plays a very vital role. Let us now re-consider the same scenario.

Read: Selenium WebDriver Quiz

Step 1 : Clicking on Link1 on Window A

A new Window B is opened.

Step 2: Save reference for Window A

Step 3 : Create reference for Window B

Step 4: Move Focus from Window A to Window B

Window B is active now

Step 5: Perform Actions on Window B

Complete the entire set of Actions

Step 6 : Move Focus from Window B to Window A

Window A is active now

Let us understand the same with a small coding example.

 public class MultiWindowHandleInSelenium {
 WebDriver driver;
 @Before
 public void setup() throws Exception {
 driver=new FirefoxDriver();
 String URL="https://www.abc.co.in/";
 driver.get(URL);
 driver.manage().window().maximize();
 }
 @Test
 public void test() throws Exception {
 // Opening Calender
 driver.findElement(By.xpath("//img[@alt='Calender']")).click();
 // Storing parent window reference into a String Variable
 String Parent_Window = driver.getWindowHandle();
  // Switching from parent window to child window
 for (String Child_Window : driver.getWindowHandles())
 {
 driver.switchTo().window(Child_Window);
 // Performing actions on child window
 driver.findElement(By.id("calendar_month_txt")).click();
 List  Months=driver.findElements(By.xpath("//div[@id='monthDropDown']//div"));
 int Months_Size=Months.size();
 System.out.println("Month size is:"+Months_Size);
 Months.get(1).click();
 driver.findElement(By.xpath("//*[@id='calendarDiv']/div/table/tbody/tr/td[contains(text(),'16')]")).click();
 }
 //Switching back to Parent Window
 driver.switchTo().window(Parent_Window);
 //Performing some actions on Parent Window
 driver.findElement(By.className("btn_style")).click();
 }
  @After
  public void close() {
  driver.quit();
  }
 }

So now let’s learn about the code in an above class to handle windows in selenium

1- driver.getWindowHandle() – This line of code is giving window handles of the current page or the parent window.

2- driver.getWindowHandles() – This line of code returns window handles of all the open windows which further can be used to switch from one window to another.

3- driver.getWindowHandles() this method returns set<string> and this is one randomly generated string for each of the windows.

4- switchTo().window(Child_Window)  - This is used to switch from one windows to another using the unique string generated for each of the window.

5- Once again is done, we need to switch focus from child window to parent window and again we will call the switchTo() and will use parent_window string.

Hope this will help you to understand, the way of handling multiple windows in selenium webdriver. But always remember for this purpose we need to be dependent on getWindowHandle for a single window and getWindowHandles for all the opened windows and for switching between each of these windows we will be using switchTo().

reference:

https://www.selenium.dev/

Shruti Shrivastava

Hey there, I'm Shruti Shrivastava. A Quality Engineer with an enthusiasm towards technical and non-technical writing. I've been into this profession for 3+ years now and thought of sharing my experiences and thus the fortunate turn of events has lead me to join Abode QA. When i am not glued to my system, i could be found reading novels, writing poems, enjoying with friends. Contact Me @ : shruti.shrivastava.in@gmail.com http://www.linkedin.com/profile/view?id=74986132&trk=nav_responsive_tab_profile

You may also like...

12 Responses

  1. Vikram Aditya says:

    Really useful stuff!But instead of using advanced for loop in getWindowHandles() we can use list or set also to iterate through the child window.

  2. Shahboz Sharipov says:

    The code you provided works perfectly fine in Firefox browser. But in Chrome it fails, because Chrome is too fast to handle this. By the time I tell it to close the child window, it closes the parent window. Is there some type of Wait method you can add to this so it waits until the child window is closed?

  3. Dao says:

    Hi,
    I got an issue when opening newwindows.
    The capabilities I set before initializing the ChromeDriver (ex. –disable-extension) is not applied to new window.

    Do you have any idea to force the opened new window inherits the capabilities of parent_window.

    Many thanks,
    Dao

  4. Santhosh says:

    HI, I am facing the multiple window issue in my project, we are using IE Driver and driver.getwindowhandles() returns value one , eventhough I opened 2 IE window, can you please help.

  5. Jayakrishnan M K says:

    When I click on a link in the Main window,the link is opening in new TAB.How can I handle the elements in the newly opened tab?

  6. sam says:

    Nice article really helpful

  7. spandana says:

    in my project i have multiple windows approx 100. now i want to close 15 th window. can you please help me

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.