Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » Handling Multiple Windows in WebDriver

Handling Multiple Windows in WebDriver

Handling multiple windows in Automation Testing has always been a little tricky and required an extra effort.

Before commencing, let us first consider a few situations when we are likely to deal with multiple windows.

  • Filling forms may require to select the date from a separately opened window.
  • Clicking on some link/button can kick-off yet another window.
  • Handling Advertisement windows

Hence, we can come up with various scenarios depending upon the application.

Now let us motion ourselves towards the challenge we face under above situations. The most particular of all is switching the focus from one window to another. Let us understand the same in the following way:

Handling Multiple Windows in WebDriver

Comprehending from the above figure, the entire process can be fundamentally segregated into following steps:

Step 1 : Clicking on Link1 on Window A

A new Window B is opened.

Step 2 : Move Focus from Window A to Window B

Window B is active now

Step 3 : Perform Actions on Window B

Complete the entire set of Actions

Step 4 : Move Focus from Window B to Window A

Window A is active now

These are the steps which we can easily interpret out of the diagram, but there are a few more steps to add to complete this process and making our script execute. These steps don’t have visibility but plays a very vital role. Let us now re-consider the same scenario.

Read: Selenium WebDriver Quiz

Step 1 : Clicking on Link1 on Window A

A new Window B is opened.

Step 2 : Save reference for Window A

Step 3 : Create reference for Window B

Step 3 : Move Focus from Window A to Window B

Window B is active now

Step 3 : Perform Actions on Window B

Complete the entire set of Actions

Step 4 : Move Focus from Window B to Window A

Window A is active now

Let us understand the same with a small coding example.

Hope this blog turns out to be helpful.

reference:

https://www.selenium.dev/

12 thoughts on “Handling Multiple Windows in WebDriver”

  1. Pingback: WebDriver’s most popular Commands | Abode QA

  2. Really useful stuff!But instead of using advanced for loop in getWindowHandles() we can use list or set also to iterate through the child window.

  3. Pingback: WebDriver's most popular Commands | Abode QA

  4. The code you provided works perfectly fine in Firefox browser. But in Chrome it fails, because Chrome is too fast to handle this. By the time I tell it to close the child window, it closes the parent window. Is there some type of Wait method you can add to this so it waits until the child window is closed?

  5. Hi,
    I got an issue when opening newwindows.
    The capabilities I set before initializing the ChromeDriver (ex. –disable-extension) is not applied to new window.

    Do you have any idea to force the opened new window inherits the capabilities of parent_window.

    Many thanks,
    Dao

  6. HI, I am facing the multiple window issue in my project, we are using IE Driver and driver.getwindowhandles() returns value one , eventhough I opened 2 IE window, can you please help.

  7. When I click on a link in the Main window,the link is opening in new TAB.How can I handle the elements in the newly opened tab?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: