-
driver.getWindowHandle():-
This method retrieves the handle of the current browser window, which serves as a unique identifier. Each browser window has a distinct handle, and this method returns a string value representing the handle of the window currently controlled by Selenium.
-
For example, if you are working with a browser window and need to retrieve its handle, you can use this method.
Example code:
pythoncurrent_window_handle = driver.getWindowHandle() print("Current window handle:", current_window_handle) -
-
driver.getWindowHandles():-
Unlike
getWindowHandle(),getWindowHandles()retrieves the handles of all open browser windows in the current session. This method returns a set of handles (typically a set of string values), containing unique identifiers for all windows. -
This method is particularly useful when switching between multiple windows or performing operations across different windows.
Example code:
pythonall_window_handles = driver.getWindowHandles() for handle in all_window_handles: print("Available window handle:", handle) if handle != current_window_handle: driver.switchTo().window(handle) print("Switched to new window:", driver.title) -
In practical applications, use getWindowHandle() when checking or operating on a specific open window. However, for test scenarios involving multiple windows—such as opening a new window from one and switching between them—getWindowHandles() is more applicable. These two methods are frequently used together in automation test scripts to effectively manage multiple browser windows.