How to verify existence of elements in page using Selenium/Java

2.1k views Asked by At

I'm trying to figure out a way to see if an element is existing/not existing on a page.

This is what I have so far.

However, if an element is not existing, an exception will be thrown, and the script will stop.

Could anyone help me find a better way to do this?

//Checking navbar links
        System.out.println("=======================");
        System.out.println("Navbar link checks");
    //Checking for Web link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a"))!= null){
            System.out.println("Web link in Navbar is Present");
            }else{
            System.out.println("Web link in Navbar is Absent");
            }
    //Checking for Images link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[2]/a"))!= null){
            System.out.println("Images link in Navbar is Present");
            }else{
            System.out.println("Images link in Navbar is Absent");
            }
    //Checking for News link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[3]/a"))!= null){
            System.out.println("News link in Navbar is Present");
            }else{
            System.out.println("News link in Navbar is Absent");
            }
    //Checking for Videos link element in Nav bar
        if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[4]/a"))!= null){
            System.out.println("Videos link in Navbar is Present");
            }else{
            System.out.println("News link in Navbar is Absent");
            }
    //Checking for Maps link element in Nav bar
           if(driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[5]/a"))!= null){
            System.out.println("Maps link in Navbar is Present");
            }else{
            System.out.println("Maps link in Navbar is Absent");
            }
2

There are 2 answers

0
Richard On

You can use a couple of different methods. For this, I'd probably recommend using findElements:

if(driver.findElements(By.xpath("/html/body/div[2]/div[1]/div[1]/ul/li[1]/a"))!= 0)
6
kiruwka On

To avoid exeptions you can simply write your own helper that will handle NoSuchElementException and return null instead :

public class Helper {
    private static final WebDriver driver;
    public static WebElement findElement(By locator) {
        try {
            return driver.findElement(locator);
        } catch (NoSuchElementException notFound) {
            return null;
        }
    }

    // initialize Helper with your driver on startup
    public static init(WebDriver yourDriver) {
        driver = yourDriver;
    }
}

Then just call it in your code :

//Checking for Web link element in Nav barl
WebElement element = Helper.findElement(By.xpath("my/complex/xpath"));
if(element == null) {
    System.out.println("My Element is Absent");
}