Showing posts with label Exception. Show all posts
Showing posts with label Exception. Show all posts

Wednesday 13 April 2016

How To Continue With Script Even If NoSuchElement Exception is Thrown

The Problem
If you selenium script fails to find element on page by provided locator, it throws NoSuchElement exception. NoSuchElement is an exception which is basically like crashing the program into a wall. Whenever this error comes, script execution gets stopped.

A Solution
As an automation developer, I can ensure uninterrupted script execution in two ways
1.       Using try/catch block

Let’s try to implement it in an example –

NoSuchElement error without Try/Catch Handling
     public static void main(String[] args) {
                                WebDriver driver = new FirefoxDriver();

                                driver.get("http://thebuddhatree.blogspot.in/");

                                driver.findElement(By.id("name")).click();
                }

Note – This program will generate error and script execution gets stopped if element with provided locator is not on page.

NoSuchElement error with Try/Catch Block

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class NoSuchElementError {

                public static void main(String[] args) {
                                WebDriver driver = new FirefoxDriver();
                                driver.get("http://thebuddhatree.blogspot.in/");
                                try {
                                                driver.findElement(By.id("name")).click();
                                } catch (NoSuchElementException e) {
                                                System.out.println("In Catch Block");
                                }
                                System.out.println("Outside Block, resume executing other code");
                }
}

Note – This program will generate error but script execution will continue for remaining code.

2.       Using FindElements() method
Build into Selenium is an ability to search multiple elements using single action
findElements() method behavior can be summarized as follows  -

·         On Zero Match : return an empty list
·         On One Match : returns list of one WebElement only
·         On One+ Match : returns list with all matching elements

NoSuchElement error using FindElements()
public static void main(String[] args) {
                                WebDriver driver = new FirefoxDriver();
                                driver.get("http://google.in/");
                               
                                List <WebElement> elements  =driver.findElements(By.id("name"));
                                if (elements.size() == 0){
                                                System.out.println("Element Not Found");
                                }
                }
Expected Behavior
  • ·         Open the Browser
  • ·         Load the page
  • ·         Search for an element
  • ·         Fails to find element
  • ·         Script execution continued

Conclusion
Happy Learning

Please ask questions on Testing Forum, in case of any issues or doubts or questions.

Saturday 5 September 2015

Selenium WebDriver : An Overview of Exception Class

List of exception classes in Selenium package (which is "org.openqa.selenium"), which we see as error on daily basis.
  1. ConnectionClosedException
  2. ErrorHandler.UnknownServerException
  3. ImeActivationFailedException
  4. ImeNotAvailableException
  5. InvalidCookieDomainException
  6. InvalidCoordinatesException
  7. InvalidElementStateException
  8. JsonException
  9. MoveTargetOutOfBoundsException
  10. NotFoundException 
  11. ScreenshotException
  12. SessionNotCreatedException 
  13. SessionNotFoundException
  14. StaleElementReferenceException
  15. TimeoutException
  16. UnableToCreateProfileException 
  17. UnableToSetCookieException
  18. UnexpectedTagNameException
  19. UnhandledAlertException 
  20. UnreachableBrowserException
  21. UnsupportedCommandException
  22. WindowsRegistryException