top of page

Common Selenium exceptions

  1. org.openqa.selenium.NoSuchElementException: this is probably the most common exception you will run into when creating Selenium tests. The reason for this exception being thrown is rather obvious: an element you were trying to interact with does not exist. Now, the reason for this can be rather direct and simple - the element simply does not exist. However, it is very common for you to think this exception was thrown erroneously, as visual inspection of the page will show that the element is actually on the page.Common reasons for this happening are listed below:

  2. typo in the webElement descriptions - make sure there are no trailing spaces inside the selector. For instance, if the webElement has an id and you provided it to the webElement descriptor, make sure that the copy/paste procedure did not add any unwanted white spaces.

  3. the element appears inside an iframe - make sure that there is no iframe top element in the HTML structure that includes the desired element or any of its predecesors. If there is such an iframe, the webElement description should remain the same, but the test code will have to switch to the corresponding iframe, before interacting with the element. This can be done with the following Selenium command:

driver.switchTo().frame("frameId");
Note that once you have finished working within the iframe, you will need to 'get out' of it, by switching back to the default content (by switching to the iframe, your visibility will be limited to the elements that are located within the iframe; trying to interact with an element from outside the iframe will throw the same NoSuchElement exception). Switching back to the default content is done in the following manner: 
driver.switchTo().defaultContent(); 
  1. org.openqa.selenium.StaleElementReferenceException: the element you are trying to access has been removed from the DOM, possibly by performing some Javascript code, between the time of finding the element in the DOM and actually using it.

  2. org.openqa.selenium.remote.UnreachableBrowserException: the browser crashed while running a test, so when the test continues to run it will not find the browser instance it started to run the test on.

  3. org.openqa.selenium.WebDriverException: unknown error: unable to discover open pages: you should upgrade your Selenium library version to the latest one, and also make sure you get the latest browser binaries.

Recent Posts

See All
bottom of page