Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify element present or visible in selenium 2 (Selenium WebDriver)

Any one can send me sample code how to verify element

  1. ispresent
  2. isvisible
  3. isenable
  4. textpresent

in Selenium WebDrvier using Java

like image 249
TEJAS TRIVEDI Avatar asked Jan 04 '13 11:01

TEJAS TRIVEDI


People also ask

How do you verify whether an element is present or not in a Web page?

isDisplayed() is the method used to verify a presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.


2 Answers

I used java print statements for easy understanding.

  1. To check Element Present:

    if(driver.findElements(By.xpath("value")).size() != 0){ System.out.println("Element is Present"); }else{ System.out.println("Element is Absent"); } 

    Or

    if(driver.findElement(By.xpath("value"))!= null){ System.out.println("Element is Present"); }else{ System.out.println("Element is Absent"); } 
  2. To check Visible:

    if( driver.findElement(By.cssSelector("a > font")).isDisplayed()){ System.out.println("Element is Visible"); }else{ System.out.println("Element is InVisible"); } 
  3. To check Enable:

    if( driver.findElement(By.cssSelector("a > font")).isEnabled()){ System.out.println("Element is Enable"); }else{ System.out.println("Element is Disabled"); } 
  4. To check text present

    if(driver.getPageSource().contains("Text to check")){ System.out.println("Text is present"); }else{ System.out.println("Text is absent"); } 
like image 92
Manigandan Avatar answered Sep 25 '22 07:09

Manigandan


You could try something like:

    WebElement rxBtn = driver.findElement(By.className("icon-rx"));     WebElement otcBtn = driver.findElement(By.className("icon-otc"));     WebElement herbBtn = driver.findElement(By.className("icon-herb"));      Assert.assertEquals(true, rxBtn.isDisplayed());     Assert.assertEquals(true, otcBtn.isDisplayed());     Assert.assertEquals(true, herbBtn.isDisplayed()); 

This is just an example. Basically you declare and define the WebElement variables you wish to use and then Assert whether or not they are displayed. This is using TestNG Assertions.

like image 33
DarthOpto Avatar answered Sep 26 '22 07:09

DarthOpto