Google+

NoAlertPresentException WebDriver Exception

What is an Alert ?

Alert is a type of pop-up, which pops up to provide important information to users.

Example for an Alert :



Before understanding NoAlertPresentException, 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 NoAletPresentException in a detailed and practical way.

NoAlertPresentException is one of the different WebDriver Exceptions and this Exception occurs, when the driver in the Selenium Program code is unable to find the Alert on the web page to switch. i.e. when the driver is switching to an invalid or non-existing Alert pop-up.  In order to work with Alert pop-ups, we have to first switch to Alert and then perform operations on Alert like reading the messages on the Alerts or Accepting the alert by pressing 'OK' button on the alert etc. Hence switching to invalid or non-existing Alert pop-up will give NoAlertPresentException.

Practical Example for NoAlertPresentExcpetion:

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



2. Right click on the 'ClickToGetAlert' button on the web page and select 'Inspect in Firepath' option  as shown below:



3.  Copy the XPath locator suggested by Firepath as shown below:



4. Write the below Selenium Code for clicking 'ClickToGetAlert' button and use the above copied XPath locator of the button in the code as shown below:



5. On executing the above code, 'ClickToGetAlert' button will be clicked and an Alert pop-up will be displayed as shown below:



6. In order to perform operations on the above shown Alert dialog, we need to write Selenium code for switching to the Alert dialog as shown below:



7. On executing the above selenium code, Selenium code will run without giving any WebDriver exceptions. But lets say, if there is no Alert pop-up available and you are trying to switch to an alert using the above marked statement, then we will get 'NoAlertPresentException' WebDriver Exception.

Comment out the Statement which will click 'ClickToGetAlert' button as shown below, so that the Alert wont be displayed:



8. Execute the above code to get 'NoAlertPresentException' as shown below, as the last statement in the above code is trying to switch to an alert, when the statement for displaying the alert is commented out:



9.  Also observe that the Selenium Code execution will stop at the line where the NoAlertPresnetException occurred and the remaining lines of code wont get executed as shown  below:



Handling NoAlertPresentException :

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



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



Hence NoAlertPresentException WebDriver Exception will be occurred, when the driver we mentioned in the Selenium Program code is unable to find the Alert Pop-up on the web page and in order to handle this, we have to use NoAletPresentException WebDriver Class in the catch block.

No comments: