Top Selenium Exceptions and How To Handle Them


Top Selenium Exceptions and How To Handle Them



I am very sure most of you might face a question like “what are the various exceptions you have faced using selenium and how to resolve this”?

so in this article, we will look into various exceptions that we will face while working with selenium and also discuss the possible solution to overcome this.

But Before that,

What is Exception?

Exceptions are an abnormal event that occurs during the execution of program/automation scripts due to which the normal flow of program/scripts will get disrupted.




Technically It is an object which is thrown at runtime and could be handled by using Try/catch block.

Now let's look into some of the most common selenium exceptions that probably you might have faced while working with selenium

WebDriverException

WebDriver Exception comes when we try to perform any action on the non-existing driver.

WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.close();
driver.quit();

NoAlertPresentException

When we try to perform an action i.e., either accept() or dismiss() which is not required at a required place it gives us this exception.

try{
driver.switchTo().alert().accept();
}
catch (NoAlertPresentException E)
{
E.printStackTrace();
}




NoSuchWindowException

When we try to switch to an window which is not present it will throws us this exception:

WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().window("Yup_Fail");
driver.close();

In the above snippet, line 3 throws us an exception, as we are trying to switch to a window that is not present.

NoSuchFrameException

Similar to NoSuchWindowException exception, Frame exception mainly comes during switching between the frames.

WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.switchTo().frame("F_fail");
driver.close();

In the above snippet, line 3 throws us an exception, as we are trying to switch to a frame that is not present.

NoSuchElementException

This exception is thrown when we WebDriver doesn’t find the web-element in the DOM.

WebDriver driver = new InternetExplorerDriver();
driver.get("http://google.com");
driver.findElement(By.name("fake")).click();

TimeoutException

Thrown when a command does not complete in enough time.



NoSuchElementException

This exception is thrown when an element could not be found.
If you encounter this exception, you may want to check the following:
        * Check your selector used in your find by...
        * Element may not yet be on the screen at the time of the find operation, (webpage is still loading).

ElementNotVisibleException

This exception is thrown when an element is present in the DOM, but it is not visible, and so is not able to be interacted with. 

To handle this exception, we can use explicit wait or try to find out the element by JavaScript. 

StaleElementReferenceException

This exception is thrown when the element no longer appears on the DOM of the page.

Possible causes of StaleElementReferenceException include, but not limited to:

       * You are no longer on the same page, or the page may have refreshed since the element was located.
       * The element may have been removed and re-added to the screen since it was located. Such as an element being relocated.
         This can happen typically with a javascript framework when values are updated, and the node is rebuilt.
       * Element may have been inside an iframe or another context which was refreshed.

SessionNotCreatedException

This exception is thrown when a new session could not be created.






SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post