How to verify if the web element exist or not?

How to verify if the web element exist or not?

In this blog post, we are going to discuss 3 different ways to verify whether the given web element is exist on the web page or not.

Verifying web elements' existence on the web page is very important in UI automation, based on which we can perform further operations in the test scripts. Let's discuss methods one by one.

Method 1: Using isDisplayed()

In this method after finding the web element, the "isDisplayed ()" selenium in-built method will be called. If the element exists, this method will return true and we assign it to a boolean variable. If the element doesn't exist "NoSuchElemenException" will be thrown.

When the exception is thrown, we are catching it in the catch block and assign a "false" value to the boolean variable.

Below is the test script written based on the above logic, the test will pass if the element exists, otherwise it will be failed.

public class VerifyIfElementExists {

    WebDriver driver;
    @Test
    public void verifyElementUsingIsDisplayedMethod(){
        boolean exist;
        try{
            WebDriverManager.edgedriver().setup();
            driver = new ChromeDriver();
            driver.navigate().to("https://www.google.com");
            driver.manage().window().maximize();
            exist = driver.findElement(By.name("q")).isDisplayed();
        }catch (Exception e){
            System.out.println("Element not found and an is exception "+e.getMessage());
            exist=false;
        }
        Assert.assertTrue(exist);
    }
}

Method 2: By finding the element

In this method, we will try to find the web element, if the element is found on the page we are assigning a "true" value to a boolean variable. Otherwise, an exception will be thrown and in the catch block, we will assign a "false" value to the same boolean variable.

This method is similar to Method 1 but the only difference is, we do not use isDisplayed() method here.

public class VerifyIfElementExists {

    WebDriver driver;
    @Test
    public void verifyElementByFindingIt(){
        boolean exist;
        try{
            WebDriverManager.edgedriver().setup();
            driver = new ChromeDriver();
            driver.navigate().to("https://www.google.com");
            driver.manage().window().maximize();
            driver.findElement(By.name("q"));
            exist = true;
        }catch (Exception e){
            System.out.println("Element not found and an is exception "+e.getMessage());
            exist=false;
        }
        Assert.assertTrue(exist);
    }
}

Method 3: Using the size of the elements

In this method, we use findElements() selenium in-built method to get all the elements on the web page of the given locator.

The logic we use here is, if the size() of the elements is not zero, the boolean variable will be assigned with "true", if it is zero "false" will be assigned.

public class VerifyIfElementExists {

    WebDriver driver;
    @Test
    public void verifyElementUsingElementsSize() {
        boolean exist = false;
        try {
            WebDriverManager.edgedriver().setup();
            driver = new ChromeDriver();
            driver.navigate().to("https://www.google.com");
            driver.manage().window().maximize();
            exist = driver.findElements(By.name("q")).size() != 0;
        } catch (Exception e) {
            System.out.println("Element not found and an is exception " + e.getMessage());
        }
        Assert.assertTrue(exist);
    }
}

These are the three different ways I know, to verify if the given web element is present or not on the given web page. If you know, of any other methods please share them in the comment section.

Thank you for reading. Keep learning. #selenium #java #brahmakothapalli

Did you find this article valuable?

Support Brahma Rao Kothapalli by becoming a sponsor. Any amount is appreciated!