Example of Handling Multiple Browser Windows in Selenium WebDriver

Selenium WebDriver software testing tool has built in "WebDriver.switchTo().window()" method available to switch from one window to another window so it is very easy to handle multiple windows in webdriver. If you remember, We can use "selectWindow" window command in selenium IDE software testing tool to select another window. If window do not have any title or both window has same title then it was very difficult to select other window in selenium IDE software testing tool. WebDriver software testing tool has made it very easy. You can handle multiple windows even if all the windows do not have any title or contains same title.

If you wants to work with multiple tabs then view THIS POST and wants to work with multiple IFrames then view THIS POST.

WebDriver.getWindowHandles()

In WebDriver software testing tool, We can use "WebDriver.getWindowHandles()" to get the handles of all opened windows by webdriver and then we can use that window handle to switch from from one window to another window. Example Syntax for getting window handles is as bellow.

Set<String> AllWindowHandles = driver.getWindowHandles();

WebDriver.switchTo().window()

WebDriver.switchTo().window() method is useful to switch from one window to another window of software web application. Example syntax is as bellow.

driver.switchTo().window(window2);

Bellow given webdriver example of switching window will explain you how selenium multi browser testing can be done. Execute it in your eclipse and try to understand how webdriver do it.

Copy bellow given @Test method part of handling multiple windows of webdriver and replace it with the @Test method part of example given on THIS PAGE. to check how multi browser testing using selenium works.(Note : @Test method is marked with pink color in that linked page).

Multiple browser in selenium test

@Test
  public void test () throws InterruptedException 
  {
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
  
  // Get and store both window handles in array
  Set<String> AllWindowHandles = driver.getWindowHandles();
  String window1 = (String) AllWindowHandles.toArray()[0];
  System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
  String window2 = (String) AllWindowHandles.toArray()[1];
  System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);
  
  //Switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
  driver.findElement(By.xpath("//input[@value='Bike']")).click();
  driver.findElement(By.xpath("//input[@value='Car']")).click();
  driver.findElement(By.xpath("//input[@value='Boat']")).click();
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);
  
  //Switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//option[@id='country6']")).click();
  driver.findElement(By.xpath("//input[@value='female']")).click();
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  driver.switchTo().alert().accept();
  Thread.sleep(5000);
  
  //Once Again switch to window2(child window) and performing actions on it.
  driver.switchTo().window(window2);
  driver.findElement(By.xpath("//input[@name='fname']")).clear();
  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name Changed");
  Thread.sleep(5000);
  driver.close();
  
  
  //Once Again switch to window1(parent window) and performing actions on it.
  driver.switchTo().window(window1);
  driver.findElement(By.xpath("//input[@value='male']")).click();
  Thread.sleep(5000);
  
  }

VIEW MORE DIFFERENT EXAMPLES OF WEBDRIVER

16 comments:

  1. can you explain this syntax please?
    et AllWindowHandles = driver.getWindowHandles();
    String window1 = (String) AllWindowHandles.toArray()[0];
    System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
    String window2 = (String) AllWindowHandles.toArray()[1];
    System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);

    ReplyDelete
    Replies
    1. Taking a set of String values named with AllWindowHandles By method getWindowHadles(), Assingning numbers to the new windows from the String values starting from zero
      then by using that numbers we can switch to that particular windows,,,

      Delete
    2. Taking a set of String values named with AllWindowHandles By method getWindowHadles(), Assingning numbers to the new windows from the String values starting from zero
      then by using that numbers we can switch to that particular windows,,,

      Delete
  2. i dont get any option to select the xpath for child window. how to inspect the element in child window with firebug??

    ReplyDelete
  3. Hi,
    driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();
    I'm unable to understand this xpath, actually couldn't understand how to create new instance of browser window? can someone please elaborate this?

    ReplyDelete
  4. You can use a website where on clicking a link/button a new window will open, for learning the windowhandle concept.

    Eg: http://www.salesforce.com/in

    ReplyDelete
  5. driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

    Could you please elobrate?

    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://only-testing-blog.blogspot.in/2013/09/test.html");
    driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

    Getting the below error:
    Unable to locate element: {"method":"xpath","selector":"//b[contains(.,'Open New Page')]"}

    Please suggest

    ReplyDelete
    Replies
    1. You have used wrong page URL.
      Use driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");

      Delete
  6. Hi,

    My question when I am working on second window, I get one more window opened so how to handle this. Because we assign the windowhandles at the beginning so if you get one more window in the middle how to handle that. Please help me out.

    ReplyDelete
    Replies
    1. Using below method we can handle as much window as per need. just pass window number from navigation starting point i.e from parent window.

      public static void switchToNewWindow(int windowNumber) {

      Set allWindowHandles =driver.getWindowHandles();

      Iterator itr=allWindowHandles.iterator();

      int i=1;
      while(itr.hasNext() && i < 10) {
      String popupHandle= itr.next().toString();
      driver.switchTo().window(popupHandle);
      actualTitle= driver.getTitle();
      System.out.println("Window title is :" + actualTitle);

      if(i== windowNumber) break;
      i++;
      }

      Delete
  7. Hi,

    I have a scenario where I am getting one more window when I click an element in the second window so could you please let me know how to handle windows when it is launched in the middle or after assigning the windowhandle at the beginning

    ReplyDelete

  8. Set AllWindowHandles = driver.getWindowHandles();

    Why only Set collection we are using why not other collection. As we all know all the windows have unique Ids so we will not have any questions of duplicates values in set.

    ReplyDelete
  9. can any one give some websites have muliple frames in single window

    ReplyDelete
  10. Element is not clickable , it would click someother element. This error is thrown.

    ReplyDelete
  11. I have a question in form page click on one link it opens 30 windows so how to switch 28 window and doing some actions in that window

    ReplyDelete
  12. provide the output with diagrams for all the ending program to know how it looks like..?

    ReplyDelete