top of page

Common Selenium Exceptions and How to Fix them

In this Blog let's go through what are exceptions and a list of common exceptions which we encounter while practicing automation test cases using Locators. The main goal of this blog is to identify the causes of the exception and find the solution to fix it.

What are exceptions?

Exception is a runtime error of an unexpected result or event in the program which interrupt usual program flow. When exceptions are unhandled, they disrupt the normal program execution.

Common Selenium exceptions

As we start write Automated tests with Selenium WebDriver, we will start facing various exceptions which will make us fussy. So, let’s go through the most common Selenium exceptions by analyzing their causes, and the possible solutions to succeed it and enjoy writing test cases with locators.

1. NoSuchElementException

The NoSuchElementException is thrown when the element cannot be found on the web page. This can happen for a number of reasons:

  • Cause 1: We are using the wrong locator.

Solution: To fix this we must make sure that we are using a locator that is both unique and correct. We need to be more careful when we choose XPATH and CSS selectors.

  • Cause 2: The web page was not completely loaded, and the element is not found.

Solution: To solve it, you can use Selenium explicit waits, and wait until the element can be found on the page. Example,

WebDriverWait wait = new WebDriverWait(driver,30);


  • Cause 3: The element is not visible on the page, and you must scroll up or down to reach it.

Solution: We can use the following JavaSCript or Action Class,

public void scrollDown()// ScrollDown the page


IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

js.ExecuteScript("window.scrollBy(0,250)", "");


Or, we can use Action class as follows,

Actions act = new Actions(driver);

act.sendKeys(Keys.PAGE_DOWN).perform(); //Page Down

act.sendKeys(Keys.PAGE_UP).perform(); //Page Up

2. ElementNotVisibleException

This one is a bit different than the NoSuchElementException because it means that the element was found in the DOM, but it is not visible on the page. This also means that we cannot interact with the element, so we can’t click, send keys, or perform other actions on it. It can happen if:

· Cause 1: The element is hidden.

· Cause 2: The locator strategy you are using finds more elements with the same locator, and the first one is not visible.

Solution: To fix this, first make sure that you are using a unique locator, and that you are identifying the correct element on the page. If your locator is good, then you can add a wait, that checks when the element is visible:

WebDriverWait wait = new WebDriverWait(driver,30);


3. InvalidSelectorException

· Cause: This Selenium exception is thrown if you are using an incorrect selector. An example of this would be using a compound class name, which is not allowed:

· Example: WebElement LoginButton = driver.FindElement(By.ClassName("login button"));

The solution for this is to make sure that the locator is correct. In the above scenario, you can replace the ClassName locator strategy with CssSelector:

WebElement LoginButton = driver.FindElement(By.CssSelector("login.button"));

4. ElementClickInterceptedException

· Cause: This Selenium exception is usually thrown when the element was found on the page, but the click action would be performed on a different element that is overlapped our element.

· Example,

So, if you have something like this in a Selenium test, where a popup message is on top of Submit Button and your test tries to click the Button you will get an ElementClickInterceptedException.

Solution: One solution to this exception is to use the Action class for performing the click:

Actions action = new Actions(driver);


Or if the element is simply out of view and scrolling will reveal it, you can use the scroll executor as shown above for the NoSuchElementException.

5. StaleElementReferenceException

Cause: The StaleElementReferenceException means that a reference to an element is now “stale”, i.e., the element is no longer available on the web page DOM. In other words, the element was initially found on the DOM, but the DOM has changed since then. The common causes for this are:

· The element was deleted from the DOM.

· The element is no longer attached to the DOM.

Solution: One possible way to fix is by refreshing the page and try to find the element again:

driver. Navigate(). Refresh();


Or you can wait for the element to load before you manipulate it. Again, you can do this using explicit waits:

WebDriverWait wait = new WebDriverWait(driver,30);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(By.Id("ElementId ")));

6. UnhandledAlertException

Cause: This exception is thrown when an alert is present on the page, preventing you from interacting with the elements.

Solution: You can dismiss or accept the alert, as needed, and move on with the test steps:

// Dismiss the alert


// Accept the alert


7. NoAlertPresentException

Cause: The NoAlertPresentException is thrown when Selenium is trying to interact with an alert that is not loaded on the webpage. Another reason can be that the alert is displayed slower than Selenium performs the actions.

Solution: In this case, you can add an implicit wait to your code, so the action performed on the alert is delayed by a few seconds:

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

8. NoSuchFrameException

Like the NoSuchElementException, this Selenium exception is thrown when a frame is not found. Again, this can happen for multiple reasons:

  • You are using an incorrect frame Id or name. In this case, make sure that you correctly identify the frame you want to switch to.

  • The frame was not yet loaded. To solve this, you can use the solution above and add an implicit wait.

  • The frame is nested inside a different frame. In this case, you must first switch to the parent frame, then to the frame you want to use.

9. NoSuchWindowException

This one is very similar to the previous exception, applied to windows. This happens when you try to switch to a new window (or tab), which cannot be found. You can solve this by getting all the windows handles, then switching to the correct one:

int windows = driver.WindowHandles;


10. TimeoutException

This exception will be thrown when the page or element was not loaded after the specified wait time. To overcome this, you can increase the wait time, if you are using an implicit wait, or better yet, replace the implicit wait with an explicit wait.

Handling exceptions with try-catch

Sometimes, the expected result is to have an exception thrown. In this case, you don’t want your code to stop executing or to throw the exception. Instead, you want to catch the exception and perform a certain action when the exception is thrown.

That means you need to handle the exception. This can be done with a try-catch block. Let’s assume you have a method that searches for the Logout button. You will use it in the test assertions, and you expect the button to be found when the login is successful, and not found when the login fails. In this case, the method will return a boolean, and if the element is not found it will throw a NoSuchElementException. Instead of the exception, you want the method to return false. This can be achieved by using the following code:

public boolean IsElementFound()





return true. true;


catch (NoSuchElementException e)


return false;// You can also add a meaningful message like "the test failed because the Logout button was not //found”



Another scenario can be that even though you don’t expect the error, you still want your program to continue running, and have the failure result logged and displayed gracefully in your test report. In this case, you can use the catch block to log the error and display a more user-friendly message, such as “The test failed because the Logout button was not found”.


Hope this Blog helps to solve the frequent exception which occurs while writing Automation test cases. Thanks For Reading!

261 views0 comments

Recent Posts

See All

Selenium Locators

What are Selenium locators? Selenium is a widely used test automation framework. Through Selenium, you can automate any interactions such as type, click, double click with the DOM WebElements, etc. To


Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page