Jump to content
We've recently updated our Privacy Statement, available here ×
  • WebElement interface methods examples in selenium - part 2 | Understanding of methods isDisplayed() Vs isEnabled() Vs isSelected()


    Hi, In this post, we will see the differences between WebElement interface most commonly used methods - isDisplayed(), isEnabled() and isSelected() with real time example scenarios.

    IsEnabled() has some special purpose of verifying elements such as buttons enabled or disabled status. Let's look into each of them.

    Image and test site courtesy : https://www.testandquiz.com/selenium/testing.html
    isDisplayed%2Bor%2BisSelected.png

    isDisplayed() 
    • To verify presence of a web-element with in the web page. 
    • This method returns either true or false boolean values. 
    • If the element is present it returns "true" otherwise it returns "false". 
    • This method avoids the problem of having to parse an element's "style" attribute.
    • This method can be applicable for almost all of the elements on web-page.
    Example:  
    HTML for : Sample
    <b xpath="1">This is sample text.</b>

    Selenium snippet
    //isDisplayed() | Text on web page | Example : This is sample text.
    WebElement ThisIsSimpleText
    = driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
    boolean b1 = ThisIsSimpleText.isDisplayed();
    System
    .out.println("Verify dispaly status of the text \"This is sample Text\"="+b1);

    isEnabled()

    • To verify if an element is enabled or disabled on web-page. 
    • Returns "ture" if element is enabled and returns "false" if an element is disabled. 
    • Examples: Mostly used with button elements, locked/disabled text input elements. 
    Example :
    Please find this blog post for a real time login use case example in which you will see "Sign Up" button is enabled after selecting a check box of terms and conditions.
    isSelected() 
    • Returns whether an element say check box or radio button is selected or not. 
    • If selected returns "true", if not selected returns "false". 
    • Examples: Check boxes, drop downs , radio buttons
    Example:
    HTML
    <input id="male" type="radio" name="gender" value="male" xpath="1">
    Selenium snippet
    //isSelected()  | Example :  male radio button
    WebElement maleRaditoButton
    = driver.findElement(By.xpath("//input[@id=male]"));
    boolean b2 = maleRaditoButton.isSelected(); //false
    System
    .out.println("Verify male radio button selected or not before click = "+b2);
    //select the male radio button and verify isSelected()
    maleRaditoButton
    .click();
    boolean b3 = maleRaditoButton.isSelected(); //true
    System
    .out.println("Verify male radio button selected or not after click = "+b3);

    Checkout this video tutorial to understand the example below

    WebElementInterfaceMethods_isDisplayedisSelectedDemo.java
    package selenium.webelement.methods;

    /* isDisplayed() , and isSelected() */

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.PageLoadStrategy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeDriverService;
    import org.openqa.selenium.chrome.ChromeOptions;

    public class WebElementInterfaceMethods_isDisplayedisSelectedDemo {

    public static void main(String[] args) {

    WebDriver driver
    ;

    //loading Chrome driver from physical drive
    System
    .setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");

    System
    .setProperty("webdriver.chrome.silentOutput", "true");
    //System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");

    //launch the browser
    driver
    = new ChromeDriver();

    //navigate to site
    driver
    .navigate().to("https://www.testandquiz.com/selenium/testing.html");

    //maximize the browser
    driver
    .manage().window().maximize();

    //isDisplayed() | Text on web page | Example : This is sample text.
    WebElement ThisIsSimpleText
    = driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
    boolean b1 = ThisIsSimpleText.isDisplayed();
    System
    .out.println("Verify dispaly status of the text \"This is sample Text\"="+b1);

    //isSelected() | Example : male radio button
    WebElement maleRaditoButton
    = driver.findElement(By.xpath("//input[@id=male]"));
    boolean b2 = maleRaditoButton.isSelected();
    System
    .out.println("Verify male radio button selected or not before click = "+b2);

    //select the male radio button and verify isSelected()
    maleRaditoButton
    .click();
    boolean b3 = maleRaditoButton.isSelected();
    System
    .out.println("Verify male radio button selected or not after click = "+b3);


    //close the browser
    driver
    .close();

    //close all the browsers opened by WebDriver during execution and quit the session
    driver
    .quit();
    }
    }



    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...