Handling multiple browser windows/popups using selenium

There are situations in which we get browser popups or multiple browser window opens, when we open an URL or click on an element on browser.

popups can be blocked in browser using some browser addons,
but for testing purpose, sometimes we need to allow the popups or multiple windows.

A single instance of selenium web-driver can handle multiple popups and browser windows.
Let’s see how web-driver handles these above mentioned popups or browser windows.

Selenium provides 2 methods getWindowHandle() and getWindowHandles() to deal with the multiple browser windows.


getWindowHandle() – this method returns the current browser window handle id that the web-driver is currently holding.

getWindowHandles() – this method returns set of browser window handle ids that are invoked/opened by the web-driver.

and we use switchTo().window(window_handle_id) to switch to different browser windows of the web-driver.



Below is the code implementation and comments of each line explanation

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: