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.
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 🙂
Sanjay says
Try-Catch will save the program to terminate but still, it will fail or Pass the Test?
Mukesh Otwani says
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.
lakshmi says
Thanks a lot
Mukesh Otwani says
Hi Lakshmi,
You’re welcome…:)
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…
Mukesh Otwani says
What do you mean by authentic mode?
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
Mukesh Otwani says
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/
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…….
Mukesh Otwani says
next article on email by this weekend.
Tom Prasad says
Thanks a lot.I was not aware of exception handling.Your article introduced me it.
Mukesh Otwani says
Cheers Mate
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.
Mukesh Otwani says
Hi Mun,
I am really glad to know it helped you 🙂 keep visiting and keep learning.
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.
Mukesh Otwani says
Next post on email coming soon.
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.
Mukesh Otwani says
Hi Pooja,
I have send the code to ur email kindly check.
Guru says
Please share the same to me: how to send the reports generated through TestNG via mail once the execution is complete
Mukesh Otwani says
Hi Guru,
Please follow below guide http://learn-automation.com/send-report-through-email-in-selenium-webdriver/
Poravikarthick says
Thanks you..It explains the concept in better manner.
Mukesh Otwani says
Thanks Poravikarthick