Selenium WebDriver: Working With The Web Elements

Posted By : Kuldeep Sidana | 24-Jun-2019

WebElement: As aspect of selenium, all the HTML elements are called WebElements. WebElements are located with the help of the locators. There are various methods used for working with the web elements. Sometimes while writing automation scripts there is a need to get text or name of the attribute and also need to verify the checkbox is selected and a button is enabled before click on it.
Following methods are used to verify the above things -
1. getText() method
2. getAttribute() method
3. isSelected() method
4. isEnabled() method
5. isDisplayed() method



1. getText() method: This method is used to get the text from the web element. Its return type is String.

2. getAttribute() method: This method is used to get the backend attribute of the web element. Its return type is also String.

3. isSelected() method: This method is used to verify whether web element is already selected or not. Its return type is boolean. Its mostly used to verify that the checkbox is enabled or not.

4. isEnabled() method: This method is used to verify whether the web element is enabled or not. Its return type is also boolean.

5. isDisplayed() method: This Method is used to verify whether the web element is displayed on a webpage or not. Its return type is boolean.

There are examples of "To verify that the checkbox is selected or not" and "To verify that a button is enabled or disabled".

 

                         import com.kairosplanning.app.scripts.BaseLib;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.Reporter;

/*
 *Created By Kuldeep Sidana on 6/26/2019
 *
 */
public class VerifyCheckboxIsSelected extends BaseLib {
    @Test
    public void addStaffTest() throws InterruptedException{
        WebDriver driver = new FirefoxDriver();
        driver.get("url");
       WebDriver checkbox = (WebDriver) ((FirefoxDriver) driver).findElementByName("chk");
        boolean flag = ((WebElement) checkbox).isSelected();
        if (flag) {
            Reporter.log("Checkbox already selected", true);
        } else {
          ((WebElement) checkbox).click();
        }
    }
}
  
        
                       import com.kairosplanning.app.scripts.BaseLib;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.Reporter;

/*
 *Created By Kuldeep Sidana on 6/26/2019
 *
 */
public class VerifyCheckboxIsSelected extends BaseLib {
    @Test
    public void addStaffTest() throws InterruptedException{
        WebDriver driver = new FirefoxDriver();
        driver.get("url");
       WebDriver loginButton = (WebDriver) ((FirefoxDriver) driver).findElementByName("button");
        boolean flag = ((WebElement) loginButton).isEnabled();
        if (flag) {
            Reporter.log("login button is enabled", true);
        } else {
            Reporter.log("login button is disabled", true);
        }
    }
}  
        
Related Tags

About Author

Author Image
Kuldeep Sidana

Kuldeep Sidana is an orient QA Engineer having dexterity in Core Java and vouched with Automation Testing tool (Selenium Web Driver) and Manual Testing.

Request for Proposal

Name is required

Comment is required

Sending message..