How to catch NoSuchElement exception in Selenium WebDriver using java?


import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class NoSuchElementexception {

 public static void main(String args[]) {
// WebDriver reference but Firefox object      
WebDriver driver = new FirefoxDriver();
//Maximize browser window       
driver.manage().window().maximize();
//Go to URL      
driver.get("http://www.google.com");
//Set  timeout      
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Catch Exception
try {
    driver.findElement(By.linkText("Gmail1")).click();
} catch (NoSuchElementException e) {
    e.printStackTrace();
}

//close firefox browser  
driver.close();
    }

}


No comments:

Post a Comment