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.

6 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Hi Nadim,

    I am having scenario where Child window is having one more child window. Could you please guide me how to handle this?

    Thanks
    R G Bhat

    ReplyDelete
    Replies
    1. Please use different driver variable for more than one child window.

      Delete

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...