Pages

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 IDEsoftware 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.

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).

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