Thursday 9 February 2017

SELENIUM: Exceptions in Selenium WebDriver


Exceptions in Selenium WebDriver:



NoSuchElementException
This exception occurs when WebDriver is unable to identify the elements during run time. Due to wrong selector or selector, which is, not exist.
Example: driver.findElement(By.id("some_id")).sendKeys("RameshSoft");

ElementNotVisibleException

This Exception occurs when the element presence is in DOM, it is not visible.

Example:-
Hidden Elements, which has presence in DOM and it, is not visible. Visibility means the height and
width should be greater than zero. Hidden Elements are defined in HTML using of type=”hidden”.
driver.findElement(By.id("hidden_id")).sendKeys("Rameshsoft");

NoSuchFrameException

This Exception occurs when the driver is switching to an invalid frame, which is not available.
Example:
driver.switchTo().frame(100);

driver.switchTo().frame("frame_abc");

NoAlertPresentException

This Exception occurs when the driver is switching to an invalid Alert, which is not available.

Example:- driver.switchTo().alert().accept();

NoSuchWindowException

This Exception occurs when the driver is switching to an invalid Window, which is not available.

Example:- driver.switchTo().window("invalidwindowname");

WebDriverException

This Exception occurs when the driver is performing the action after immediately closing the browser.

Example:-
driver.close();
driver.findElement(By.id("Email")).sendKeys("rameshsoft.selenium@gmail.com");

SessionNotFoundException

This Exception occurs when the driver is performing the action after immediately quitting the browser.

Example:-
driver.quit();
driver.findElement(By.id("Email")).sendKeys("rameshsoft.selenium@gmail.com ");

StaleElementReferenceException

This Exception occurs when the Element belongs to a different frame than the current one. The user has navigated away to another page.
Thrown when a reference to an element is now “stale”.
Stale means the element no longer appears on the DOM of the page.
Possible causes of StaleElementReferenceException include, but not limited to:
·         You are no longer on the same page, or the page may have refreshed since the element was located.
·         The element may have been removed and re-added to the screen, since it was located. Such as an element being relocated. This can happen typically with a javascript framework when values are updated and the node is rebuilt.
·         Element may have been inside an iframe or another context which was refreshed.
Example:
WebElement element=driver.findElement(By.id("username"));// Element is available in parent window
driver.switchTo().window(Child_Window);//Switch to Child Window
element.sendKeys("Ramesh");//perform the action on the element which is not visible in the child window
TimeoutException:
Exception thrown when a command does not complete in enough time.
UnableToSetCookieException:
Exception Thrown when a driver fails to set a cookie.
UnexpectedAlertPresentException:
Exception thrown when an unexpected alert is appeared.

NoSuchAttributeException:
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. (IE8’s .innerText vs. Firefox .textContent)
ElementNotSelectableException:
Thrown when trying to select an unselectable element.

No comments:

Post a Comment