Friday 14 February 2014

How to verify if the element is displayed or not using selenium webdriver in Java?

Selenium webdriver provides one method called - isDisplayed which can be used to check if any element like button, checkbox, link etc  is displayed or not on web page in Selenium Webdriver in Java.

boolean actualValue = e.isDisplayed();
above code will check if button e is displayed or not. If it is displayed, actualValue will be true otherwise it will be false.

Full example in Java with selenium webdriver

package temp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class first {

public static void main(String[] args) {
              // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver""C:\\Selenuim\\chromedriver2.3.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.get("http://register.rediff.com/register/register.php");

Thread.sleep(2000);
WebElement e = driver.findElement(By.name("btnemail"));

boolean actualValue = e.isDisplayed();

if (actualValue)
       System.out.println("Button is displayed");
else
       System.out.println("Button is not displayed");
      
Thread.sleep(2000);

}

catch(Exception ex){
       System.out.println("Exception " + ex.getMessage());
              }
              finally{
                    
                     driver.close();
                     driver.quit();
              }
       }

}


Please note that we can verify if all elements are displayed or not like button, checkbox, radiobutton, checkbox, combobox in similar way using isDisplayed method in Java using selenium webdriver.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

2 comments:

  1. WebElement e = driver.findElement(By.name("btnemail"));

    boolean actualValue = e.isDisplayed();

    if (actualValue)
    System.out.println("Button is displayed");
    else
    System.out.println("Button is not displayed");

    Thread.sleep(2000);


    if it is in loop then according to different data that element will not show
    and it will throw error unable to locate element

    ReplyDelete
    Replies
    1. for that you can use try catch block right!!

      Delete

Buy Best Selenium Books

Contributors