Selenium WebElement Interface

Introduction to WebElement Interface

Once we find an element using the findElement() or findElements() method of the WebDriver interface, we can perform various operations on it using the WebElement interface methods.
The WebElement interface basically represents an HTML element on a web page. It provides methods for interacting with elements, such as clicking, sending keys, and retrieving text.

Syntax to use WebElement Interface

To use the WebElement interface, we first need to find the element using the findElement() method of the WebDriver interface. Here is the syntax to use the WebElement interface:
WebDriver driver = new ChromeDriver();
WebElement element = driver.findElement(By.id("elementId"));

WebElement Methods Details

We will now see the details of some of the important methods of the WebElement interface:
  • click(): This method is used to click on the element.
    WebElement element = driver.findElement(By.id("elementId"));
    element.click();
    click() method can be used on any element that is clickable like a button, link or checkbox.
  • sendKeys(CharSequence... keysToSend): This method is used to enter text into a text field or text area. Technically, This method is used to send a sequence of keystrokes to the element.
    WebElement element = driver.findElement(By.id("elementId"));
    element.sendKeys("Hello World");
    This method can be used on any element that accepts text input like a text field or text area.
  • getText(): This method is used to get the visible text of the element.
    WebElement element = driver.findElement(By.id("elementId"));   
    String text = element.getText();
    This method is used to retrieve the text of a label or paragraph.
  • isDisplayed(): This method is used to check if the element is displayed on the web page.
    WebElement element = driver.findElement(By.id("elementId"));
  • isEnabled(): This method is used to check if the element is enabled on the web page.
    WebElement element = driver.findElement(By.id("elementId"));
  • isSelected(): This method is used to check if the element is selected on the web page.
    WebElement element = driver.findElement(By.id("elementId"));
  • getAttribute(String name): This method is used to get the value of the specified attribute of the element.
    WebElement element = driver.findElement(By.id("elementId"));
    String value = element.getAttribute("attributeName");
    This method is used to retrieve the value of an attribute like id, name, or class.
  • getCssValue(String propertyName): This method is used to get the value of the specified CSS property of the element.
    WebElement element = driver.findElement(By.id("elementId"));
    String value = element.getCssValue("propertyName");
    This method is used to retrieve the value of a CSS property like color, font-size, or background-color.
  • clear(): This method is used to clear the content of the element if it is a text entry element.
    WebElement element = driver.findElement(By.id("elementId"));
    element.clear();
    This method is used to clear the text from a text field or text area.

WebElement Methods Applicable on Different Elements

Different types of web elements support different methods. Here are some examples:
Element Type Methods
Button click(), isDisplayed(), isEnabled()
Text Field sendKeys(CharSequence... keysToSend), clear(), getAttribute(String name)
Checkbox click(), isSelected(), isDisplayed()
Dropdown click(), getText(), getAttribute(String name)
Link click(), getText(), getAttribute(String name)

WebElement methods code example

Let's see an example of how to use the click() method to click on a button:
WebDriver driver = new ChromeDriver();
WebElement button = driver.findElement(By.id("buttonId"));
button.click();

Method Description
click() Clicks the element.
sendKeys(CharSequence... keysToSend) Sends a sequence of keystrokes to the element.
getText() Returns the visible text of the element.
isDisplayed() Returns true if the element is displayed, false otherwise.
isEnabled() Returns true if the element is enabled, false otherwise.
isSelected() Returns true if the element is selected, false otherwise.
getAttribute(String name) Returns the value of the specified attribute of the element.
getCssValue(String propertyName) Returns the value of the specified CSS property of the element.
clear() Clears the content of the element if it is a text entry element.
Next we will learn Actions class and its methods.