Handling multiple windows
- We get NoSuchElementException, if we try to perform operations on a new window, without switching to it during Selenium Automation
- Windows can be opened either as new window or new tab.
- WebDriver interface has two predefined methods getWindowHandles() and switchTo() , which helps us in handling multiple browser windows.
- getWindowHandles() – Used to retrieve all the ids of the currently opened windows (Windows can be popup-windows, windows opened in new tabs etc)
- switchTo() – Used to switch between different windows when multiple windows are opened by using the ids of the currently opened windows which are returned by getWindowHandles()
- Syntax: switchTo().window(“Retrieved Window ID”);
- Writing Programming logic for handling more windows
- Closing active window using close()
- Closing all windows using quit()
- Helper method for handling windows
- From Selenium 4, apart from just switching to new window or tab, we can also create a new window or new tab and then switch to them.
- driver.switchTo().newWindow(WindowType.WINDOW)
- driver.switchTo().newWindow(WindowType.TAB)
- We can open multiple applications at the same time and automate, because of this feature.
- Practical Demonstration
- NoSuchWindowException – Practical Demonstration
By,
Arun Motoori