Exceptions in Selenium

No Such Element Exception
Web Driver cannot find an element, in such cases you will get the No such element 
Exceptionbydefault web driver will wait certain amount of time for an element, after that also element not visible on the application web driver will through an exception.

Different causes to get No such element Exception.
1) Element not be found in the application/page (means declared incorrect locator in script).
2) Page is still being rendered (Ex: ajax pages)
3) Element may not yet be on the screen at the time of the find operation,

If you add different wait statements in your scripts, you can avoid such type of exceptions
Few are 1) Implicity wait, 2) Explicity wait 3) FluentWait

Ex1:
Driver.manage().timeouts().implicitlywait(10,TimeUnit.Seconds)

Ex2:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));


Ex3:
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);


Ex4:
public boolean isElementPresent(By selector) {
return driver.findElements(selector).size()>0;
}


Ex5:
public boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}






No Such Frame Exception

If page has multiple frames and user trying to access the objects of other frames, without switching to another frame, in such cases, we will get No Such Frame Exception. We have different ways to handle the frames depends on the need.

driver.switchTo().frame(int param1);
where param1 is index
ex: driver.switchTo().frame(0);
in the above example we are accessing the frame is based on index.
ex:
public void frameexample(int frames) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame id " + frames);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame id " + frames
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frameids " + frames
+ e.getStackTrace());
}
}

driver.switchTo().frame(String param1);
ex: driver.switchTo().frame(String param1);
Select a frame by its name or ID.


No Such Window Exception
When driver is switching to invalid window , which is not available , in such case No Such Window Exceptionoccurs
To find the current set of active window handles, you can get a list of the active window handles
Ex1 :Driver,switchTo().window(0)
Ex2: Driver,switchTo().window(“sameple”)



StaleElementReferenceException
A stale element reference exception is thrown in one of two cases, the first being more common than the second:
1) The element has been deleted entirely.
2) The element is no longer attached to the DOM.
WebElement element = driver.findElement(By.id("example"));
String text = element.getText();
If "element.getText" returns before the element is removed from the DOM you'll get one result. If, however, the element is removed from the DOM and your code does an automatic lookup for the element again before "element.getText" a different result may be returned.



InvalidElementStateException
Web driver can't type text in a disabled field , in such cases it will throw InvalidElementStateException

Unsupport commandException
Indicate that a command used by the remote web driver is unsupported.

Unreachable browserException
Indicates there was a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception are:
1) The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
2) The browser has died mid-test.
3) session not getting killed, try to kill the session and run the test again.
4) selenium web driver cannot find the FF browser to start it.
5) FF and Webdriver version should be compatible to each other. Change your Webdriver jars and try it.
6) There may be unwanted jar present in your project. Remove it
7) Check all your client drivers and selenium-server.jar are placed on the libraries.

Element Not visible exception
If you are trying to access particular element on the page that is not currently visible, in suchcase you will get the Element not visible exception.Most commonly encountered when trying to click or read text of an element that is hidden from view.



SessionNotFoundException
when you are running your tests, things will go wrong and you will lose connection with the browser instance that you are driving. When you lose
connection to the browser instance, a SessionNotFoundException will be thrown.
problems usually cause this error:
• unintentional quit the driver instance
• The browser crashed

NoAlertPresentException
Indicates that a user has tried to access an alert when one is not present.
This can be caused by calling an operation on the Alert() class when an alert is not yet on the screen.
Ex: driver.switchTo().alert().accept();

ElementNotSelectableException
This exception thrown when trying to select an unelectable element.

ErrorInResponseException
This exception is thrown when an error has occurred on the server side.
This may happen when communicating with the firefox extension or the remote driver server.

InvalidSelectorException
This exception thrown when the selector which is used to find an element does not return a WebElement. Currently this only happens when the selector is an xpath expression and it is either syntactically invalid (i.e. it is not a xpath expression) or the expression does not select WebElements (e.g. “count(//input)”).

InvalidSwitchToTargetException
This exception thrown when frame or window target to be switched doesn’t exist.

NoSuchAttributeException
This exception thrown when the attribute of element could not be found.
You may want to check if the attribute exists in the particular browser you are testing against. Some browsers may have different property names for the same property.(IE:innerText vs. Firefox .textContent)

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

UnexpectedAlertPresentException
This exception thrown when an unexpected alert is appeared.
Usually raised when when an expected modal is blocking webdriver form executing any more commands.

UnexpectedTagNameException
This exception thrown when a support class did not get an expected web element.

WebDriverException
This exception thrown when code is unable to initialize WebDriver.
Ex: driver.close();
driver.findElement(By.id("loginuser")).sendKeys("hello");

Comments