• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to Handle Exception in Selenium Webdriver

How to Handle Exception in Selenium Webdriver

March 28, 2015 by Mukesh Otwani 22 Comments

Handle exception in Selenium

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 🙂

 

 

Filed Under: Basic Selenium Tagged With: Exception handling in Selenium Webdriver

Reader Interactions

Comments

  1. Sanjay says

    February 27, 2022 at 9:30 AM

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

    Reply
    • Mukesh Otwani says

      March 9, 2022 at 9:49 AM

      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.

      Reply
  2. lakshmi says

    September 11, 2019 at 4:29 PM

    Thanks a lot

    Reply
    • Mukesh Otwani says

      September 12, 2019 at 10:21 AM

      Hi Lakshmi,

      You’re welcome…:)

      Reply
  3. sudhr says

    September 30, 2016 at 7:02 AM

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

    Reply
    • Mukesh Otwani says

      October 3, 2016 at 4:33 PM

      What do you mean by authentic mode?

      Reply
  4. Sandeep says

    September 28, 2016 at 11:32 PM

    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

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:36 AM

      Hi Sandeep,

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

      Kindly refer http://learn-automation.com/how-to-write-dynamic-xpath-in-selenium/ and http://learn-automation.com/capture-error-message-in-selenium/

      Reply
  5. sudhr says

    September 21, 2016 at 6:28 AM

    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…….

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:31 PM

      next article on email by this weekend.

      Reply
  6. Tom Prasad says

    September 4, 2016 at 7:17 PM

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

    Reply
    • Mukesh Otwani says

      September 7, 2016 at 11:59 PM

      Cheers Mate

      Reply
  7. Mun says

    August 17, 2016 at 4:05 PM

    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.

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 9:31 AM

      Hi Mun,

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

      Reply
  8. Phani says

    June 9, 2016 at 2:44 AM

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

    Reply
    • Mukesh Otwani says

      June 14, 2016 at 9:22 PM

      Next post on email coming soon.

      Reply
  9. Pooja says

    March 31, 2016 at 3:20 PM

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

    Reply
    • Mukesh Otwani says

      April 1, 2016 at 5:04 PM

      Hi Pooja,

      I have send the code to ur email kindly check.

      Reply
      • Guru says

        December 13, 2016 at 10:08 PM

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

        Reply
        • Mukesh Otwani says

          December 13, 2016 at 11:24 PM

          Hi Guru,

          Please follow below guide http://learn-automation.com/send-report-through-email-in-selenium-webdriver/

          Reply
  10. Poravikarthick says

    March 16, 2016 at 5:38 PM

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

    Reply
    • Mukesh Otwani says

      March 20, 2016 at 7:33 PM

      Thanks Poravikarthick

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?