Reputation: 375
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?
Upvotes: 2
Views: 17633
Reputation: 1
Selenium has its own exception-handling package:
from selenium.common.exceptions import StaleElementReferenceException
try:
#your code
except StaleElementReferenceException:
#exception
Upvotes: 0
Reputation: 24447
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.
Upvotes: 2
Reputation: 11326
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
}
Upvotes: 2