In this article we learn the how to handle window in selenium webdriver and what is the difference between getWindowHandle() & getWindowHandles().
How To Handle Window In Selenium WebDriver?
A window handle is a unique identifier that is assigned to each window created. Selenium WebDriver provides us two methods getWindowHandle () and getWindowHandles () which are used to get window handle/s.
1. getWindowHandle ():
It returns an opaque handle to this window that uniquely identifies it within this driver instance.
We can switch to any window using its unique window handle.
Window handle is alphanumeric.
Return type of getWindowHandle () is a String type.
2. getWindowHandles ():
It returns a Set of window handles open by particular driver instance.
It returns a Set because handle of each window is unique and Set allows only unique elements.
Using this method, we can iterate over any window open by current instance.
return type of getWindowHandles () is Set<String>
Syntax: Switch to other window
driver. switchTo (). Window ();
switch to(): This method helps to switch between the windows
Which exception is thrown when we provide wrong window handle?
We will get the NoSuchWindowException.
What is the difference between getwindowhandle () & getwindowhandles ()?
getwindowhandle (): This is used to get the address of the current browser window where it’s focused on and returns the data type of String.
getwindowhandles (): This is used to get the address of all the open browsers and returns the data type of Set<String>.