Exceptions we face while working with Selenium

Exception-

An exception is an event which occurs during the execution of a program that disrupts the normal flow of the program, and causes the program to abruptly end. This can loss of data, time, or corrupt files or database. Ideally a good program should have exception handling in place before being delivered for execution.

Test Automation is to generate the test script for executing the complete end to end flow. During this script creation and execution, tester will face the same problem as the developer face during development cycle. These scripts are tested by tester, but tester need to focus on some factors for gracefully execution. One of them is to avoid exceptions during execution. Let us see in here what can we face and how to avoid it -

Unexpected popups- When we execute our automation script, sometimes application toss an alert and until you handle these alert windows you are not able to perform any action in the parent window.

            For example: In web developments Signup scenarios, promotional popups are common which can appear any time during execution. Without handling them,  we cannot proceed. So, these types of alerts are blocking our automation script and web driver throw an exception. Selenium provides a separate interface for it called as Alert, https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/Alert.html

And by using its methods we can handle these alerts.

 

 

 

 

Object or page not found and disabled- “Object and page not found exception” occurs when object or page not loaded completely during script execution. Generally this error is thrown as “No Such Element Found” exception. Suppose our network is slow and we are executing our script, in the middle of execution, script is trying to find an object which is not loaded on page. At this time the findElement method will throw the exception. 

To handle this type of exception, we should use the concept of synchronization at the script level with help of implicit waits, explicit waits etc.

Not accessible or stale objects- In current web development, different web technologies are used due to which this happens. When an element is no longer attached to the DOM, i.e. it has been removed from the document or the document has changed.

            We face the stale element reference exception when the element we are interacting is replaced with another one. This element may look identical, but it is different. Web Driver may get confused with element and throw Stale Element Exception.

For handling this you can use below solutions:

1.      You can refresh your page and try to find same element.

2.      May be element is not attached on DOM it will take time, so you can use the “try-catch block” within “for loop”.

3.      We can handle this using Page Object model, as it will reduced our effort of maintaining the script for object related issues. You can read more about it here - http://5elementslearning.com/moredetail/WHAT-IS-PAGE-OBJECT-MODEL-IN-SELENIUM

Script Error- This is due to the incorrect code written by tester in automation script. This may happen due to divide by zero, file related issue, array related issues like accessing array with wrong dimension, accessing array with an index, which is outside the legal boundaries and many others.

To handle this type of exception it’s better to recheck out test script. And use the try catch block at the code which could be prone to error/exception. 

Challenges with Internet Explorer- While we are executing our automation script on internet explorer we need to focus on below points otherwise Selenium Web driver throws an exception.

  • Unexpected error Browser zoom level: This is very common error during execution on Internet Explorer. We must set the IE Browser zoom level to 100% by default to overcome this error.
  • Protected Mode settings are not the same for all zones: To avoid this exception  we need to set the protected mode setting Same for all the zones.

 

 

 

Different type of Exceptions in Selenium WebDriver

Below is a list of different types of exceptions which can be raised while working with selenium – 

 

Explanation -

  • NoAlertPresentException

This Exception occurs when the driver is switching to an invalid Alert, which is not available.

  • NoSuchFrameException

This Exception occurs when the driver is switching to an invalid frame, which is not available.

  • NoSuchwindowExcpetion

This Exception occurs when the driver is switching to an invalid Window, which is not available.

  • NoSuchElementException

This exception occurs when WebDriver is unable to identify the elements during run time.

  • ElementNotInteractableException

This exception occurs when an element is present in the DOM and it is not in a state that can be interacted with.

  • ElementNotSelectableException

This exception occurs when trying to select an unselected element.

  • ElementNotVisibleException

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

  • JavaScriptException

This exception occurs while executing JavaScript supplied by the users.

  • TimeOutException

This exception occurs when a command does not complete in enough time.

  • ScriptTimeOutException

This exception occurs when an async execute script command does not complete in enough time.

  • StaleElementReferenceException

This exception occurs when reference to an element is not “stale” which means the element is no longer appears in the DOM.

  • UnsopportedCommandException

This exception occurs when command used by the webdriver is unsupported.

 

Hope you found this article helpful, and it helps you in creating more robust scripts. Happy Automation.

 

 About Author  

Niharika, has an overall 6 years of experience in software testing. She has worked on varied applications like Desktop application, E-Commerce Web Application, Mobile Application and Casino games. An  ISTQB Certified Tester, she currently resides at  San Fransisco Bay Area, USA