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
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);
}
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.