Tuesday 16 July 2019

Different Types of Exceptions in Selenium + Java

Exceptions in Selenium WebDriver
Exception general meaning is a condition that is not common. Exceptions in Selenium WebDriver occurs many times in different scenarios. Following are few of them listed with example.

1. 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("invalidid")).sendKeys("Testing");

2. 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("hiddenid")).sendKeys("Testing");

Reason 1- Duplicated XPATH
While writing xpath for your application, you might have taken xpath that is matching with more than 1 element, in this case, Selenium will throw Element, not the visible exception.

Solution: Try to write unique XPATH  that matches with a single element only.

Reason 2:
If you are trying to access some particular element on Webpage that is not currently visible, in this case also you will get the Element, not visible exception.

Solution: Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.
Syntax for Explicit wait
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='index.html']")));

Reason 3 with solution:
This actually works for me a number of times. I was struggling with the OK button in my application. Even I was writing unique xpath but still I was facing Element Not Visible exception.  I have tried below code that solved my issue.

int ok_size=driver.findElements(By.xpath("//button[text()='OK']")).size();
driver.findElements(By.xpath("//button[text()='OK']")).get(ok_size-1).click();

3. NoSuchFrameException:
This Exception occurs when the driver is switching to an invalid frame, which is not available.
Example:-
driver.switchTo().frame(invalidindex);
(or)
driver.switchTo().frame("frame_z");

4. NoAlertPresentException:
This Exception occurs when the driver is switching to an invalid Alert, which is not available.
Example:-
driver.switchTo().alert().accept();
//Execute this command on browser without invoking the alert.

5. NoSuchWindowException:
This Exception occurs when the driver is switching to an invalid Window, which is not available.
Example:-
driver.switchTo().window("invalidwindowname");

6. WebDriverException:
This Exception occurs when the driver is performing the action after immediately closing the browser.
Example:-
driver.close();
driver.findElement(By.id("username")).sendKeys("Testing");

7. SessionNotFoundException:
This Exception occurs when the driver is performing the action after immediately quitting the browser.
Example:-
driver.quit();
driver.findElement(By.id("username")).sendKeys("Mukesh");

8. StaleElementReferenceException:
This Exception occurs when the Element belongs to a different frame than the current one. The user has navigated away to another page.
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("Mukesh");//perform the action on the element which is not visible in the child window

9. InvalidElementStateException:
when element is in disabled mode, we can encounter this exception
WebDriver driver=new ChromeDriver();

driver.get("http://www.testing.com");

// This element is disable, so if we try to click on disable webelement then it will throw exception
// Using below code it will pass the data forcefully.

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript("document.getElementById('pass').value = 'Testing';");

10. ElementNotClickable:

// Scroll the browser to the element's Y position
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");

11. illegalstateexception:
  java.lang.illegalstateexception
 When we don't use system.setpropery method for Chrome, IE, Mozilla this exception comes
System.setProperty("webdriver.ie.driver","path of the chromedriver.exe");
WebDriver driver=new InternetExplorerDriver();

12. stateelementexception:
How to solve Stale Element Reference exception

Solution 1:
You can refresh the page and again try for the same element.
Example- If you are trying to click on link and getting the exception then try in below format
Driver.navigate().refersh();
Driver.findElement(By.id(“ur element id”)).click();

Solution 2:

Sometimes it takes the time to attach element on Dom so you can retry using for loop and try catch.
for(int i=0i<=2;i++)
{
  try{
     Driver.findElement(By.id()).click();
    break;
}
catch(Exception e)
{
Sysout(e.getMessage());
}
}

No comments:

Post a Comment