As Selenium only automates Web Applications, we will be mostly dealing with Browser Windows. Browser Window is a square box in which browser generally display the web pages in it.
Example for Window:
Before understanding NoSuchWindowException, you have to first understand the below:
NoSuchWindowException is one of the different WebDriver Exceptions and this Exception occurs, when the driver we mentioned in the Selenium Program code is switching to an invalid pop-up window. As Selenium only automates Web Applications, we will be mostly dealing with Browser Windows. Browser Window is a square box in which browser generally display the web pages in it. In order to work with the Web Elements on any pop-up window, we have to first switch to the pop-up window using Selenium and then locate the respective web elements inside the pop-up window. NoSuchWindowException WebDriver Exception occurs, when the driver in the Selenium Program code is unable to find the pop-up window on the web page to switch. i.e. when the driver is switching to an invalid or non-existing pop-up window.
Practical Example for NoSuchWindowExcpetion:
1. Open Firefox Browser and open http://www.omayo.blogspot.com as shown below:
2. Create an XPath for 'Open a popup window' link on the web page to locate in Selenium code as shown below:
3. Write the below Selenium Code for clicking 'Open a popup window' link and use the above created XPath locator of the link in the code as shown below:
4. On executing the above code, 'Open a popup window' link will be clicked and a pop-up window will be opened as shown below:
5. In order to perform operations on the above opened pop-up window, we need to write Selenium code for find the pop-up windows and switching to the pop-up window as shown below:
6. On executing the above selenium code, Selenium code will run without giving any WebDriver exceptions. But lets say, if there is no pop-up window available and you are trying to switch to it using the above marked statement, then we will get 'NoSuchWindowException' WebDriver Exception.
Provide a non-existing window Id string in the Code as shown below:
7. Execute the above code to get 'NoSuchWindowException' as shown below, as the switchTo statement in the above code is trying to switch to a non-existing popup window id :
8. Also observe that the Selenium Code execution will stop at the line where the NoSuchWindowException occurred and the remaining lines of code wont get executed as shown below:
Handling NoSuchWindowException :
1. If you want those statements to be executed, even after the Exception has occurred in the before statement, we need to handle the Exception using Exceptional Handling mechanism:
i.e. We have to use try .. catch blocks to handle the exception and also 'NoSuchWindowException' WebDriver Exception Class needs to be used in the catch block as shown in the below code:
2. Hover the mouse over the 'NoSuchWindowException' error in the above image and select 'import NoSuchWindowException org.openqa.selenium ' option from the suggested resolutions as shown below:
3. Observe that the error gets resolved as shown below and execute the code:
4. Observe that the Exception got handled successfully and statements after the Exception got executed as a result of handling the exception. The below screen-shot is the proof that the NoSuchWindowException is handled using Exception handling mechanism and the statement after the handled exception also got executed:
Hence NoSuchWindowException WebDriver Exception will be occurred, when the driver we mentioned in the Selenium Program code is unable to find the Pop-up window on the web page and in order to handle this, we have to use NoSuchWindowException WebDriver Class in the catch block.