2

How does Selenium handle exceptions? I am using Selenium from last few months and I am facing a problem as my test case used to run in very uneven manner. Sometimes it throws the exception and when I run the same test case again it executes in the orderly manner. Is this an error or an exception?

1
  • Can you please mention the problem specifically? What error/problem have you found in console or log? Commented Jan 27, 2015 at 13:04

3 Answers 3

2

You can use the exception handling in whatever language you are using to interface with webdriver.

WebDriver driver = new InternetExplorerDriver();
try
{
    // do something with webdriver, e.g.
    driver.get("http://localhost/");
    driver.findElement(By.name("btn")).click();
}
catch (Exception)
{
     // Handle exception, ignore it or log it
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is a poor way of handling exceptions, you should be more specific about what exception you expect.
How is this supposed to work if the exceptions are thrown asynchronously?
2

Your Selenium tests should be able to fail, but not because of exceptions that are thrown. If your tests are failing from exceptions then quite likely you have no exception handling. By doing this, you don't have the opportunity to cleanup the WebDriver object.

The tests should be failing under your terms. This is a generalisation though since it depends on how your tests are written and what the exceptions being thrown are. For example, you should never be getting exceptions like NullPointerException but if you are getting such as ElementNotFoundException, then this may be due to the page not loading fast enough. In this case you would increase the implicit wait time. If a truly exceptional case does occur where an exception is thrown then you should decide how to handle it. Whether you are going to rethrow it later at the end of the test, print some error logging, etc.

4 Comments

Mike,I had made two methods for handling the timed out errors ,as I thought it was due to the Thread.and yes I was right as I am able to resolve my issue of Timed out,but not able to handle the problem which i discussed in my previous question.
Katie, how did you resolve your time out issue? I am also struggling with timed out, will you show your code?
public void run() { try { Thread.sleep(timeout * 1000); /* Timeout occurred / ThreadReturn.save(new TimeoutException("Time out occured waiting for page to load")); / Stop WebDriver.get */ Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_ESCAPE); } catch (InterruptedException ex) { return; } catch (AWTException ex) { System.out.println("Error occurned pressing ESC"); ex.printStackTrace(); }
'{ final private int timeout; public TimeoutThread(int seconds) { this.timeout = seconds; }'
0

Selenium has its own exception-handling package:

from selenium.common.exceptions import StaleElementReferenceException

try:
    #your code
except StaleElementReferenceException:
    #exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.