Selenium NoSuchWindowException is similar to NoSuchFrameException, this is also one of the frequent exceptions which we will face in selenium webdriver. Sometimes applications will have child windows(i.e. Separate windows) which will open when you click on any link or button. Normally if you want to automate the child windows then you need to switch to that window then only you can interact with the elements which are there on that window. To overcome this, we need to handle the opened windows using one of the webdriver methods called “driver.getWindowHandles()”. Once you have this information then you can switch to the corresponding window to automate or interact with the window.
Below are the causes to face this exception:
- This exception is caused because the list of windows hold by the driver.getWindowHandles() method is not up to date. One of the windows that previously existed is no longer exists; so, you can not switch to that window. To handle this we need to make sure that you are not closing a window without refreshing the available list of windows using this method.
- The other reason behind this exception is that you may trying to switch to window before handling all the opened windows using driver.getWindowHandles() method. And we need to keep in mind that we should know which window we are trying to switch as getWindowHandles() holds all the windows which are opened. To get the main window then you can use one the webdriver methods called “driver.getWindowHandle()”. Once you have all the windows with you then you can loop through all the windows which are there in the list and we can switch to the desired window.
Once you done with your work, then you need to come to the parent or default windows then only your execution will be smooth. This way can overcome the NoSuchWindowException in selenium webdriver.
Please watch the YouTube video for this blog for better understanding.