Delivered by FeedBurner

Selenium WebDriver : Verify Element Present In Selenium WebDriver

Some times you need to verify the presence of element before taking some action on software web application page. As you know, Selenium IDE has many built in commands to perform different types of actions on your software web application page. You can verify presence of element by using "verifyElementPresent" command in selenium IDE software testing tool. Also you can view example of  selenium IDE software testing tool's "verifyElementNotPresent" command. Web driver have not any built in method or interface by which we can verify presence of element on the page of software web application.

Yes we can do it very easily in WebDriver too using bellow given syntax.
Boolean iselementpresent = driver.findElements(By.xpath("//input[@id='text2']")).size()!= 0;

We have to use findElements() method for this purpose. Above syntax will return true if element is present on page of software web application. Else it will return false. You can put if condition to take action based on presence of element on page of software web application.

Bellow given example will check the presence of different text box on page. It will print message in console based on presence of element.

Copy bellow given @Test method part of iselementpresent example and replace it with the @Test method part of example given on THIS PAGE(Note : @Test method is marked with pink color in that linked page).

 public void test () throws InterruptedException 
  for (int i=1; i<6; i++)

   //To verify element is present on page or not.
   String XPath = "//input[@id='text"+i+"']";
   Boolean iselementpresent = driver.findElements(By.xpath(XPath)).size()!= 0;
   if (iselementpresent == true)
    System.out.print("\nTargeted TextBox"+i+" Is Present On The Page");
    System.out.print("\nTargeted Text Box"+i+" Is Not Present On The Page");


  1. Textbox 1 Is present on the page

    Textbox 2 Is present on the page

    Textbox 3 Is not present on the page

    Textbox 4 Is present on the page

    Textbox 5 Is not present on the page

    Why is textbox 4 present on the page while texbox 3 is not present on page ?. Please check

    1. in this program we are searching element by xpath . So if we check .//*[@id='text3'] & .//*[@id='text5'] xpath does not correspond to any element on the page. Hence system is giving valid error message for 3 & 5

  2. Can you give example text present but text that i want, not by size number.

  3. Boolean iselementpresent = driver.findElements(By.xpath(XPath)).size()!= 0; is tailing time, is there any way to get the response immediately?

  4. Able to implement it successfully.. The main point to remember is .. its driver.findElements and not driver.findElement.