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. Webdriver have not any built in method or interface by which we can verify presence of element on the page of software web application. There is not iselementpresent method in Selenium WebDriver.

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

To check iselementpresent in selenium webdriver, We have to use findElements() method. 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).

@Test
 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");
   }
   else
   {
    System.out.print("\nTargeted Text Box"+i+" Is Not Present On The Page");
   }
  }
 }
This way you can check if iselementpresent in selenium webdriver.

6 comments:


  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

    ReplyDelete
    Replies
    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

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

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

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

    Thanks

    ReplyDelete
  5. I used your method, it's fine, it returns true/false.

    While returning True, it finds the element pretty quickly and comes back. But when trying false it takes 5.5 - 6 seconds.

    What would be the reason?

    ReplyDelete