Exceptions in Selenium WebDriver

Exceptions in Selenium

Last updated on

In this post, we will study the most commonly encountered exceptions in Selenium WebDriver and the root cause of these exceptions.

NoSuchElementException

Reason – In case no element could be located from the locator provided.
Resolution – Check the correctness of the locators for the elements and make sure that the element is present when interacting with them.

ElementNotVisibleException

Reason – In case the element is present in the dom but is not visible.
Resolution – Make sure that the element is in the visible area when interacted with. Some common methods to achieve this are – maximizing the browser window, and scrolling to the element.

NoAlertPresentException

Reason – In case we try to switch to an alert but the targetted alert is not present.
Resolution – Make sure that alert is present when it is interacted with.

NoSuchFrameException

Reason – In case we try to switch to a frame but the targetted frame is not present.
Resolution – Check the frame locators and make sure the frame is present on the webpage.

NoSuchWindowException

Reason – In case we try to switch to a window but the targetted window is not present.
Resolution – Get the list of window handles using driver.getWindowHandles() and switch to one of the handles present at that particular time.

UnexpectedAlertPresentException

Reason – In case an unexpected alert blocks the normal interaction of the driver.
Resolution – Accept or dismiss the alert to continue interacting with the dom.

TimeoutException

Reason – In case a command execution gets a timeout.
Resolution – This may be a valid exception unless we have set very low timeout values in implicit and explicit waits.

InvalidElementStateException

Reason – In case the state of an element is not appropriate for the desired action.
Resolution – Make sure that the element is available to perform the desired operation by waiting for the desired ExpectedCondition in explicit wait.

NoSuchAttributeException

Reason – In case we are trying to fetch an attribute’s value but the attribute is not correct.
Resolution – Just make sure the attribute we want to fetch from an element is actually present in the element or not.

WebDriverException

Reason – In case there is some issue with the driver instance preventing it from getting launched.
Resolution – Check the driver’s instantiation and the dependencies required to instantiate the driver object.

This completes the tutorial on – Exceptions in Selenium. During your Selenium interviews, the interviewer might also ask about these different types of exceptions, one has encountered during automation.

Leave a Comment