Google+

NoSuchFrameException WebDriver Exception

What is an iframe ?

iframe is a HTML web page inside another HTML web page.



Example for iframe:



Before understanding NoSuchFrameException, you have to first understand the below:


So, after understanding 'What is an Exception and the different types of Exceptions in Selenium WebDriver', you are now ready to understand NoSuchFrameException in a detailed and practical way.

NoSuchFrameException 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 frame. iframe is a HTML web page inside another HTML web page. In order to work with the Web Elements on any iframe, we have to first switch to the iframe in Selenium and then locate the respective web elements inside the iframe. NoSuchFrameException WebDriver Exception occurs, when the driver in the Selenium Program code is unable to find the frame on the web page to switch. i.e. when the driver is switching to an invalid frame.

Practical Example for NoSuchFrameExcpetion:

1. Open Firefox Browser and open http://www.omayo.blogspot.com as shown below:



2. Right click on any Web Element inside the iframe say 'Se' love shape image and select 'Inspect Element with Firebug' as shown below:



3. Observe that the HTML code for the 'Se' love share image will be displayed under the Firebug's HTML tab as shown below:



4. Scroll up in the HTML code and find out the parent 'iframe' html tag under which the 'img' html tag highlighted in the above screenshot, as shown below:



5. Observe that the 'iframe' tag has 'id' attribute as shown below:

Note: We can use the below show 'id' attribute value for switching to the iframe:



6. Write the Selenium Code for switching to the 'iframe' having the above 'id' attribute value as shown below and execute the code:



7. Observe that the above Selenium Code is able to switch to the iframe and click on 'What is Selenium?' link inside the iframe.

8. Now, provide an invalid or not available 'id' attribute value for switching to iframe, by modifying the code as shown below and execute the code :



9. On execute the Selenium code in the above image having invalid id locator for locating the iframe,  NoSuchFrameException WebDriver Exception will be displayed in the Eclipse IDE as shown below:



10. Also observe that the Selenium Code execution will stop at the line where the NoSuchFrameException occurred and the remaining lines of code didn't get executed as shown  below:



Handling NoSuchFrameException :

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 'NoSuchFrameException' WebDriver Exception Class needs to be used in the catch block as shown in the below code:





2. Hover the mouse over the 'NoSuchFrameException' error in the above image and select 'import NoSuchFrameException 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 NoSuchFrameException is handled using Exception handling mechanism:



Hence NoSuchFrameException WebDriver Exception will be occurred, when the locators (i.e. id / xpath/ css selectors etc) we mentioned in the Selenium Program code is unable to find the iframe on the web page and in order to handle this, we have to use NoSuchFrameException WebDriver Class in the catch block.

No comments: