How to handle the multiple windows in selenium ?

In real time application whenever you perform any action on element then it might do following things.

1. It opens a new browser window.

2. It opens a new tab on same browser window

Handling new browser window or child browser window.

During run time if we click on any element present in the browser and it opens a child browser window then webdriver cannot access child browser window directly because the control will be still in the main browser.

In order to make webdriver to work on child browser window and performed the required action we need to follow below mention steps

1. First we need to uniquely identify the current browser window by using getWindowHander() , return type of this method is string

/*get the home window id for future comparisons*/

String homeWindowId=driver.getWindowHandle();

2. Now Perform the action on the element after which you receive an additional browser window.

3. In order to retrieve all the the browser window id which are open by webdriver used a method getWindowHandlers().

/*get the browser window ids opened by webdriver*/

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

4. There are two possible ways to get the id of child browser window

Logic 1:- After getting the all window id used iterator to get an instance of iterator interface as mention below

Set<String>allWinId=driver= driver.getWindowHandlers();

Iterator<String> itr=allWinId.iterator();

OR

Iterator<String> itr =driver. getWindowHandlers().iterator();

In order to retrieve child windowed from iterator of String type we will write below mention logic

String tweetWindowId=null;

int i=0;

while(i<allWinId.size())

{

String tempId=itr.next();

if(!tempId.equals(homeWindowId))

{

tweetWindowId=tempId;

}

i++;

}

After getting child Window id we can make webdriver to switch to child browser window and performed the required operation.

Logic 2- After getting the all window id remove the howe window id from allWinId.

allWinId.remove(homeWindowID);

After the above statement allWinId will have only one browser id i.e. child window id.

So in order to move webdriver control from home window to child window used below mention statement.

Note: As allWinId is a set type of collection hence in order to retrieve child window id We need to take a help iterator and then we have use next() of iterator to fetch the child window id

driver.switchTo().window(allWinId.iterator().next());

Now after switching the control to child browser window we can performed any required action on child browser window

5. Once you done all child window operation and you want to performed some action on home browser window then used below mentioned statement

driver.switchTo().window(homeWindowId);

After this statement the control will be in the main browser window and we can performed any operation on this windows

6. In order to close the all window browser opened by webdriver we can used following method

driver.quite()

QA Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store