Check Web element Visibility Using Selenium WebDriver Commands – IsSelected IsDisplayed IsEnabled
How to check Web Element visibility using Selenium WebDriver:
To check the web element visibility using Selenium WebDriver, we do use the following methods. We could check the web element visibility using Selenium such as buttons, checkboxes, radio buttons, drop down boxes etc.,
- Boolean isSelected(): This method is used to verify the element is selected or not. This method returns a true value if the specified element is selected and false if it is not selected.  It is widely used on checkboxes, radio buttons, and options in a select.
boolean elePresent = driver.findElement(By.xpath("xpath")).isSelected();
- Boolean isDisplayed(): This method is used to verify the presence of the element. It determines if an element is displayed or not. This method returns a true value if the specified element is displayed and false if it is not displayed.
boolean eleSelected= driver.findElement(By.xpath("xpath")).isDisplayed();
- Boolean isEnabled(): This method is used to verify whether the element is enabled or not. It returns a true value if an element is enabled and false if it is not enabled.
boolean eleEnabled= driver.findElement(By.xpath("xpath")).isEnabled();
Let’s see a practical example to check web element visibility using Selenium WebDriver.
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package softwareTestingMaterial; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class TestTestTest { public static void main (String [] args) throws InterruptedException { //Instantiation of driver object. To launch Firefox browser System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //To open gmail driver.get("http://www.gmail.com"); //isDisplayed() method returns boolean value either True or False Boolean Display = driver.findElement(By.xpath("//*[@id='next']")).isDisplayed(); //To print the value System.out.println("Element displayed is :"+Display); //isEnabled() method returns boolean value either True or False Boolean Enable = driver.findElement(By.xpath("//*[@id='next']")).isEnabled(); System.out.println("Element enabled is :"+Enable); //Passing value as "softwaretestingmaterial" in the email field driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("softwaretestingmaterial"); //to click on next button driver.findElement(By.xpath("//*[@id='next']")).click(); //isSelected() method returns boolean value either True or False Boolean Select = driver.findElement(By.xpath("//*[@id='PersistentCookie']")).isSelected(); System.out.println("Element selected is :"+Select); } }
Execute the above script and see the console output.
Also Read: Extent Reports in Selenium WebDriver
If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.