How to check if element / object is present on webpage?

In this article we will talk about, to check if particular element present on webpage or not.

In some cases, it is required to verify if element exist, click on the element if it exist.
Let us see how this can be achieved.
             1.       First Navigate to website.
             2.       We will use findElements here, so that we can check for all element with particular locator is present on webpage.
            3.       Get all the elements in a list.
            4.       If list is not empty, this means, element exist
            5.       If list is empty, this means, element is not present on webpage.
Look at the below example.
package basicsOfSelenium;

import java.util.concurrent.TimeUnit;

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

public class CheckIfElementPreesntOnWebPage {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://demowebshop.tricentis.com/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();

  if (driver.findElements(By.xpath("//a[@class='ico-login1']")).size() != 0 )
    {
   System.out.println("Element is present on WebPage");
  }
  else {
   System.out.println("Element is not present on webpage");
  }
 }

}

Refer below video –

Hope above example helps !!!

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories