• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Selenium Programs » How to Multiple Window Handling in Selenium WebDriver?

How to Multiple Window Handling in Selenium WebDriver?

Last Updated on: August 19, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Window Handling In Selenium

Switch to Multiple Window || Window Handling In Selenium Example Program: How to Switch to Multiple Window Using Java Selenium Program?

In today’s post, we are going to discuss one of the common scenarios which you may face during the interview that is appearing in multiple windows when you click on some elements on a web page.

Sometimes the focus is still on the current browser, but when you try to do some operation, it will not work, and you will get some errors. You are getting such errors because the selenium webdriver is not able to uniquely identify the opened browser.

This is happening because when we open any browser window that time selenium webdriver assigns a unique value to that window for uniquely identify that window, but when multiple windows are opens that time selenium webdriver is not able to locate the browser window.

To Handle multiple browser windows, Selenium webdriver interface provides two methods, that are:

  • getWindowHandle(): This method will return a string value of the current parent browsers window handle.
  • getWindowHandles(): This method will return a set of all windows handles that are open in that session.

With these two methods, we can use the switchTo() method to switch from one window to another window by providing the different window handle.

Window Handling In Selenium

package com.selenium.switchto;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Switchto_MultipleWindow 
{
   WebDriver driver=new FirefoxDriver();
   @BeforeTest
   public void open()
   {
      driver.manage().window().maximize();
      driver.get("http://popuptest.com/");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      
   }
   @Test
   public void operation() throws InterruptedException
   {
      String parent=driver.getWindowHandle();
      driver.findElement(By.xpath("html/body/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[1]/font[1]/b/a")).click();
      Thread.sleep(5000);
      ArrayList<String> handles=new ArrayList<String>(driver.getWindowHandles());
      System.out.println(handles.size());
      for(int i=0;i<handles.size() ;i++)
      {
         String str=handles.get(i).toString();
         if(str.equals(parent))
         {
            System.out.println(i+ "Parent Popup opened");
            System.out.println("This Is Your Parent Window");
            driver.switchTo().window(parent).close();
            System.out.println(i+ "Parent Popup closed");
         }
         else
         {
            System.out.println(i +"popup is opened");
            Thread.sleep(1000);
            driver.switchTo().window(str).close();
            System.out.println(i +"popup is closed");
         }			
      }
   }
}

    Filed Under: Selenium Programs

    Reader Interactions

    Comments

    1. Avatar for TanmayTanmay says

      August 20, 2019 at 1:32 PM

      Can you plz provide solution How to Switch(Specific windows) to Multiple Window…for examples there is 10 window and I want to switch on 4th one……….while handling the windows….I’m trying this by using get index method of the list but not getting the required output

      Reply

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers