Monday, 24 June 2013

Handling switch over from one window to another using Selenium Webdriver

This script helps you to switch over from a Parent window to a Child window and back cntrl to your Parent Window

String parentWindow = driver.getWindowHandle();
Set<String> handles =  driver.getWindowHandles();
for(String windowHandle  : handles)
{
if(!windowHandle.equals(parentWindow))
   {
     driver.switchTo().window(windowHandle);
      <!--Perform your operation here for new window-->
driver.close(); //closing child window
    driver.switchTo().window(parentWindow); //cntrl to parent window
    }
}

3 comments:

  1. could you please tell me how to handle 3 window

    ReplyDelete
  2. Iterator windowIterator1 = driver.getWindowHandles().iterator();
    while (windowIterator1.hasNext())
    {
    String windowHandle = windowIterator1.next();
    driver = driver.switchTo().window(windowHandle);

    if(driver.getTitle().contains("Document Explorer"))
    {
    }
    }

    ReplyDelete