How to switch-handle-close particular popup window, When multiple popups are opened?



  • 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 {
            @emmalee       public void test5() throws Exception {
              WebDriver driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.get("http://site2.way2sms.com/content/prehome.jsp");
    
                String mainwindow=driver.getWindowHandle();
                driver.findElement(By.linkText("Contact us")).click();
    
                for(String winHandle :driver.getWindowHandles()){
                    driver.switchTo().window(winHandle);
                    if(driver.getTitle().equals("Real Estate | Property in India | Buy/Sale/Rent Properties | MagicBricks")){
    System.out.println("You are in required window");
                        break;
                        } else{
                System.out.println("Title of the page after - switchingTo: " + driver.getTitle());
                    }
                }
                driver.switchTo().window(mainwindow);
                driver.quit();
        }
          }
    


  • 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/

    @emmalee public void PopupUsingSet() throws InterruptedException{
    driver.get("http://www.popuptest.com/");
    driver.findElement(By.xpath("//a[@href='popuptest12.html']")).click();
    String CWH=driver.getWindowHandle();
    System.out.println("Current Window Handle: "+driver.getWindowHandle());
    
        //Adding the windowhandles to a set
    s.addAll(driver.getWindowHandles());
    
        //removing CWH from the set as we want to take actions only on the poopups
        s.remove(CWH);
    
        //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();
    while(ii.hasNext()){
        //elements inside the set are of the type object, so we convert them to string 
            driver.switchTo().window(ii.next().toString());
        System.out.println(driver.getTitle());
        driver.manage().window().maximize();
        System.out.println(driver.findElement(By.xpath("//img")).getAttribute("src"));
        driver.close();
                 }
        //bringing the control back to the main window
        driver.switchTo().window(CWH);
    

    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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2