7

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 {
        @Test
      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();
    }
      }
5
  • 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

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/

@Test
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.

4
  • 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 that you have read and understand our privacy policy and code of conduct.

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