i

Selenium Step By Step Guide

IsElementPresent, IsEnabled, IsSelected

Checking if an element is present or not?

When it comes to validating if the element is present on the web page or not, we need to handle this situation by looking for the element using the findElement() method from WebDriver instance. Once we get the element, then we can for the element value, and if it is not null, then it means the element is present on the web page.

We can see below snippet where we need to check for the presence of "software and it" text on the page.

So, let us see the code to check if the element is present on the web page or not:

package SeleniumTest.SeleniumTest;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class ElementPresent

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        WebElement element=driver.findElement(By.xpath("//h2[contains(.,'software and it')]"));

        if(element!=null)

        {

               System.out.println("Element is present on the web page");

        }

        else       

        {

                System.out.println("Element is not present on the web page");

        }

    } 

}

IsEnabled Method:

Now we face some situation while validation where we need to check whether the element is enabled or not. This can be validated easily by using the WebElement method “IsEnabled".  This method takes nothing as a parameter and returns a Boolean value.

We can again take the https://selflearning.io/ web page to validate the “software and it” link text to validate if this is enabled or not.

Let us see the sample code to check the same:

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class ElementIsEnabled

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        driver.get("https://selflearning.io/");

        WebElement element=driver.findElement(By.xpath("//h2[contains(.,'software and it')]"));

        if(element.isEnabled())

        {

               System.out.println("Element is enabled on the web page");

        }

        else       

        {

               System.out.println("Element is not enabled on the web page");

        }

    } 

}

IsSelected Method:

We see situations where we need to check the Checkbox or radio buttons and need to validate if a checkbox or a radio button is already selected or not. This situation can be handled easily by using the "IsSelected” method.

This method takes nothing as a parameter and returns a Boolean value.

package SeleniumTest.SeleniumTest;

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class ElementIsSelected

{

    public static void main( String[] args )

    {

        WebDriver driver=new ChromeDriver();

        driver.get("http://www.echoecho.com/htmlforms10.htm");

        List <WebElement> elements=driver.findElements(By.xpath("//table[3]/tbody/tr/td/table/tbody/tr/td/input[@name='group2']"));

        int iSize=elements.size();

        System.out.println(iSize);

        for(int i=0;i<iSize;i++)

        {

               if(!elements.get(i).isSelected())

               {

                              

                               elements.get(i).click();

                              

               }

        }

    } 

}