Tuesday, August 12, 2014

How to handle multiple windows in Selenium Webdriver

Now a days multiple windows are used common in most of the web applications. In some cases clicking on a link to open the page in a new windows. Selenium webdriver object needs to move to that window and do some operation and back to the previous window. So it is important to switch to the specific window recognizing its Title, Element, Content etc.
Selenium webdriver has given some specific features to handle multiple windows in a browser. For example, we have WindowA. Clicking on a link of WindowA will open a WindowB and clicking on a link of WindowB will open a new WindowC.
Now we want to move from WindowA to WindowC, the code (Java) will be,
String Parent_Window=driver.getWindowHandle();

 for (String Child_Window : driver.getWindowHandles())
         {
             driver.switchTo().window(Child_Window); 
         }

driver.getWindowHandle() :: returns the current window position.
driver.getWindowHandles() :: Rturns all the open windows referrence.

Using For statement driver will hover all window and stop to the last WindowC. If we want to move back to WindowA, we need another statement,
driver.switchTo().window(Parent_Window);

After moving to a window, we can do any other operation on that window and then moves to other windows.