Basic Selenium

How to Handle Exception in Selenium Webdriver

Hello Welcome to Selenium Tutorial, today we are going to discuss how to Handle Exception in Selenium Webdriver without using much code.

In Selenium, we are going to face so many exceptions some of them are listed here.

Frequent Exceptions in Selenium

What is Exception?

An exception in Java in an event, which can disturb our program flow or you, can say it can terminate our program. An exception can come during execution of the program so we should follow better exception handling in our program/script.

 

Handle Exception in Selenium

 What is Exception handling?

Exception handling is a concept in the programming language to save our program from terminating. Let me explain in with the help of simple example

int c=5/0;

If we execute our program then at run time definitely, it is going to throw an exception called ArthematicException

Like this, we have the multiple exceptions in Selenium as well so let us see how we can handle this.

 

Handle exception in Selenium

In Java, we can handle exception using try- catch block.

Syntax: 1- try with 1 catch

try

{

 // Code which can throw exception

 }

 catch(ExceptionClass objectname)

 {

 // Take necessary actions when exception come

 }

 

 

 

Syntax 2- try with multiple catch block

try

 {

 // Code which can throw exception

 }

 catch(ExceptionClass1 objectname)

 {

 // Take necessary actions when exception come

 }

 catch(ExceptionClass2 objectname){

 // actions

 }

 catch(ExceptionClass3 objectname){

 // actions

 }

 

 

Note- Make sure Parent Exception should come in last catch block otherwise we will face code, not reachable error.

Scenario for Selenium-

Consider a scenario in which we have to find an element with the help of XPath, id,name etc. and if Selenium is not able to find that element then it will throw NoSuchElementException and it will terminate your program, so in this we have to use try-catch block so that we can save our program being terminated.

Syntax-

try{

driver.findElement(By.id("someid")).click();

}

 catch(NoSuchElementException e){

 Sysout(e.getMessage());

}

 

 

We have the same exception in java.util also so make sure you have to import following package

 import org.openqa.selenium.NoSuchElementException;

For better automation practice always, try to use Exception handling.

 

Hope you like above post. Please comment if you finding any issue.

Please share with others as well. Have a nice day 🙂

 

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

22 thoughts on “How to Handle Exception in Selenium Webdriver

  1. Sanjay says:

    Try-Catch will save the program to terminate but still, it will fail or Pass the Test?

    1. Hi Sanjay, if you handle with catch then it will mark as pass but in catch if you throw some exception then it will mark as fail.

  2. lakshmi says:

    Thanks a lot

    1. Hi Lakshmi,

      You’re welcome…:)

  3. sudhr says:

    is there any another solution to handle nosuch element exception?other than try catch block.if u run the testcase in authentic mode…

    1. What do you mean by authentic mode?

  4. Sandeep says:

    Hi Mukesh,

    At first i would like to appreciate your work and the efforts you put in.
    I have some issue with gmail page. In detail,

    When an user enters invalid username in “Enter your email” field and click “next” button, the page will generate and error text. I am unable to find that element by element locator. I have tried with xpath, ID but got an NoSuchElementexception.
    It would be helpful if you let me know hand to find element with standard Element locators.

    Thanks in advance,
    Sandeep

    1. Hi Sandeep,

      Kindly use contains method of xpath or Text() method of xpath. I always use this to capture error messages.

      Kindly refer https://vistasadprojects.com/mukeshotwani-blogs-v2/how-to-write-dynamic-xpath-in-selenium/ and https://vistasadprojects.com/mukeshotwani-blogs-v2/capture-error-message-in-selenium/

  5. sudhr says:

    is there any another solution to handle NO such element exception?and I want to know how to send the reports generated through TestNG via mail once the execution is complete.Please help…….

    1. next article on email by this weekend.

  6. Tom Prasad says:

    Thanks a lot.I was not aware of exception handling.Your article introduced me it.

  7. Mun says:

    woow
    I faced many problems previously in my script and i did not solve because i dont know how to handle exception. After read your article it is now vwey clear and easy to me to solve exception error.

    Thanks.

    1. Hi Mun,

      I am really glad to know it helped you 🙂 keep visiting and keep learning.

  8. Phani says:

    Hi Mukesh,
    I want to know how to send the reports generated through TestNG via mail once the execution is complete.Please help.

    1. Next post on email coming soon.

  9. Pooja says:

    Hi Mukesh,
    I want to know how to send the reports generated through TestNG via mail once the execution is complete.Please help.

    1. Hi Pooja,

      I have send the code to ur email kindly check.

      1. Guru says:

        Please share the same to me: how to send the reports generated through TestNG via mail once the execution is complete

  10. Poravikarthick says:

    Thanks you..It explains the concept in better manner.

    1. Thanks Poravikarthick

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.