Monday, February 9, 2015

Selenium Exceptions why it occurs and how to resolve/avoid them

While using selenium for web automation many a time we face numerous kind of exceptions. Many of these exceptions are mainly selenium exceptions. In this blog I will write about few of those selenium exceptions, why they occur and how to resolve/avoid them.

  • StaleElementReferenceException - 
    As the name suggests this exception occurs when the element stale, which means the element reference on which you are trying to take a action upon is no longer available on the page or has changed. 
    To avoid this exception, try to find the element as and when you need to take an action upon it rather than getting the element at some point of code and then reusing it at different places.

  • InvalidCoordinatesException - 
    This exception occurs when invalid co-ordinates are provided for an interaction operation like move, click, drag & drop etc.
    You can avoid this exception by identifying a Webelement on the page for interaction rather than providing a co-ordinates.

  • ElementNotVisibleException - 
    This exception occurs when an element may be available in the dom but not displayed on the page and hence not able to be interacted. This exception may also occurs if the element is unable to scroll onto the browser view.
    You can avoid this exception by verifying that the element is visible and then taking action on the element. There are some methods present in "ExpectedConditions" class that can be used to wait for the element to be visible. These methods are "visibilityOf(Webelement)" and "visibilityOfElementLocated(By)".

  • InvalidSelectorException -
    This exception is thrown when the selector used to find the element is invalid or wrong, this may be a syntax error or a compound class name to be provided to "By.className" method.
    To avoid this exception verify that the selector that you are using are verified on the said page using jQuery or firebug or firepath.

  • NoSuchElementException -
    This is a common exception faced by selenium users and occurs when there is no element found on the current page by selenium based on the user provided selector to method "findElement". There can be multiple reasons for this exception to occur like selector being wrong, element taking time load(in case of ajax), earlier step execution failed etc.
    You can avoid this exception by implementation by waiting for element to be present using "FluentWait" with "ExpectedConditions" class methods "presenceOfElementLocated(By)" and "visibilityOfElementLocated(By)".

  • UnreachableBrowserException -
    This exception mainly occurs due to two reasons first when the browser to which the selenium commands has died mid-test and second when the remote server address to which the RemoteWebDriver trying to contact is invalid.
    There is no proper solution to this error you can only take precautionary measures in this cases and is more or less related to hardware.


Unknown said...

Thanks a lot for your support

Post a Comment