How to check visibility or appearance or presence of web elements in Selenium Webdriver using Java

Sometime We don't know about the element visible or no that reason we need to check web element is present or visible or enabled or disabled etc. Selenium Webdriver facilitates the user with some methods to check the visibility of the web elements. isSelected(), isEnabled() and isDispalyed() etc methods are used to determine the visibility scope for the web elements.

driver.findElements(By.id("email")).size() ! =0
We need to perform a action based on a specific web element being present or not on the web page. We can return true only when the size of elements is greater than Zero. That mean there exists at least one element.
driver.findElements(By.id("email")).size()>0; or
driver.findElements(By.id("email")).size() ! =0;


isEnabled
Below is the syntax which is used to check if the element is enabled or not
WebElement element = driver.findElement(By.id("email"));
element.isEnabled();


isDisplayed
Below is the syntax which is used to check if the element is displayed or not. It returns false when the element is not present in DOM.
WebElement element = driver.findElement(By.id("email"));
element.isDisplayed();


Demo Source Code
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PresentElement {

public static void main(String[] args) {
// create objects and variables instantiation     
WebDriver driver = new FirefoxDriver();
// maximize the browser window       
driver.manage().window().maximize();
// launch the firefox browser and open the application url     
driver.get("http://www.facebook.com");
//Set  timeout      
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//To check Element Present
if (driver.findElements(By.id("email")).size() != 0) {
    System.out.println("Element is Present");
} else {
    System.out.println("Element is Absent");
}
//or
if (driver.findElements(By.id("paaswprd")).size() > 0) {
    System.out.println("Element is Present");
} else {
    System.out.println("Element is Absent");
}

//To check Visible
if (driver.findElement(By.id("u_0_v")).isDisplayed()) {
    System.out.println("Element is Visible");
} else {
    System.out.println("Element is InVisible");
}

//To check Enable
if (driver.findElement(By.id("u_0_1")).isEnabled()) {
    System.out.println("Element is Enable");
} else {
    System.out.println("Element is Disabled");
}
// fetch the source of the web page and save it into a string variable
String title = driver.getPageSource();
//To check text present
if (title.contains("Sign Up")) {
    System.out.println("Text is present");
} else {
    System.out.println("Text is absent");
}

//close firefox browser  
driver.close();
    }

}


2 comments:

  1. How to switch focus to new window using javascriptexector

    ReplyDelete
  2. Using Javascriptexecutor iam able to click on hidden element but it switching to new window iam not able to focus the new window and enter value in text box

    ReplyDelete