I know how to switch to single popup window, but I do not know how to switch particular popup window when you have more popup windows.

Scenario is: Go to the each and every popup

  1. Get the title
  2. Perform any action (Click on link) on popup
  3. Close the particular popup.

Test URL: http://site2.way2sms.com/content/prehome.jsp

Below is the sample code:

  import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;

public class PopupRnD {
      public void test5() throws Exception {
          WebDriver driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

            String mainwindow=driver.getWindowHandle();
            driver.findElement(By.linkText("Contact us")).click();

            for(String winHandle :driver.getWindowHandles()){
                if(driver.getTitle().equals("Real Estate | Property in India | Buy/Sale/Rent Properties | MagicBricks")){
System.out.println("You are in required window");
                    } else{
            System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
  • What seems to be the problem? By the looks of it, this piece of code would work fine. It will do exactly what you wanted to get done.
    – Yash
    Apr 28, 2014 at 13:10
  • @Yash: I want to switch to a particular popup window (When you have more than 1 popup windows) and perform some actions or close it.
    – QA4it
    Apr 28, 2014 at 14:33
  • @QA4it- You have answered the question in the question itself. The sample code above is the perfect example. The 'for' loop enables you to iterate over every window. Combined with the 'if' condition you can pick a window that suits you and take action over it. If you need another example then i can post one where I use a Java Set or List to do the same thing.
    – Yash
    Apr 28, 2014 at 15:28
  • @Yash: Please post your code. Thanks Yash :)..
    – QA4it
    Apr 28, 2014 at 15:54
  • @Yash, how are you initializing variable s in your example. I am unable to get the popup window handle.
    – user11846
    Apr 29, 2015 at 10:01

1 Answer 1


So here's my code. Not much different from your sample code. I have used a Java Set to store the window handles of pop ups(since driver.getWindowHandles() returns a set)

Test url: http://www.popuptest.com/

public void PopupUsingSet() throws InterruptedException{
String CWH=driver.getWindowHandle();
System.out.println("Current Window Handle: "+driver.getWindowHandle());

    //Adding the windowhandles to a set

    //removing CWH from the set as we want to take actions only on the poopups

    //iterating over the popups. Going to each popup, getting the title and the src 
   //of the image displayed and closing them        
    Iterator ii= s.iterator();
    //elements inside the set are of the type object, so we convert them to string 
    //bringing the control back to the main window

Hope this helps. I have not used the Java List because it requires unnecessary effort in splitting the list element into usable window handles.

Looking at your example code and the one i wrote it's clear that using an advanced for loop is the more efficient method.

  • 1
    I forgot to mention one thing. Elements inside the Java Set are automatically sorted according to their values so iterating over them would result in the control being shifted to random pop-ups. It wont follow the sequence in which the pop-ups opened.
    – Yash
    Apr 29, 2014 at 12:57
  • Why using an advanced "for" loop is more efficient? Why usage of iterators is considered advanced method?
    – dzieciou
    Jun 29, 2014 at 15:49
  • According to JavaDoc, getWindowHandles() returns Set<String>, so there is no reason to call toString() method. Instead, it would be enouth to declare your s variable as Set<String> and iterator as Iterator<String>, as well.
    – dzieciou
    Jun 29, 2014 at 15:55
  • @Yash - popuptest.com was very much useful Mar 30, 2015 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.