DEV Community

Deepikandas
Deepikandas

Posted on

Finding WebElements and WebElement commands

What is findElement() in Selenium?

findElement() is a WebDriver method used to locate a single web element on a web page using a locator.
What it actually does.
WebElement can be of any type, like it can be a Text, Link, Radio Button, Drop Down, WebTable or any HTML element.

When you call findElement():

Selenium sends request to browser driver (ChromeDriver, etc.)
Driver searches the DOM (Document Object Model)
It finds the first matching element
Returns it as a WebElement object

πŸ‘‰ That WebElement is your β€œhandle” to interact with the element.
🧩** Syntax**

findElement(By locator)
Enter fullscreen mode Exit fullscreen mode

1. Return Type: **WebElement
findElement() returns a single WebElement object
That object represents the first matching element in the DOM
**2. Parameter:
By locator
βœ” Meaning:
It accepts a By object
By is a locator strategy class in Selenium

What is By?
By is a class used to define how Selenium should locate an element
Examples:

By.id("username")
By.name("email")
By.xpath("//button[@id='login']")
By.cssSelector("#login")
WebElement Commands in Selenium

A WebElement represents an element on the webpage, and Selenium provides methods to interact, inspect, and verify it.

1. SendKeys Command
βœ” Purpose:
is mainly used to enter/input data into editable web elements.
It is commonly used for:
Text fields
Password fields
Text areas
Search boxes
sendKeys() is for giving input (typing data),** not for checkbox/radio button selection.**
βœ” Parameter: CharSequence... keysToSend
This means:
πŸ‘‰ It accepts varargs (variable arguments)
πŸ‘‰ Each argument must be a CharSequence
What is CharSequence?

CharSequence is an interface in Java that represents a sequence of characters.
Common implementations:
String
StringBuilder
StringBuffer
So these are ALL valid:
element.sendKeys("admin"); // String
element.sendKeys(new StringBuilder("a")); // StringBuilder
⚑** Why varargs (...)?**

Because you can send multiple inputs in one call:
element.sendKeys("user", "name");
πŸ‘‰ Selenium internally concatenates them as:username
βœ”Return type: Nothing

2. clear() Command in Selenium
⚑ Method Signature
void clear()
🧠 What does clear() do?

πŸ‘‰ clear() is used to remove existing text from an input field (text box).

It deletes any pre-filled or previously entered value.

3.click() Command in Selenium
⚑ Method Signature
void click()
πŸ‘‰ click() simulates a real user mouse click(left) on a web element.
It is used to interact with:
buttons
links
checkboxes
radio buttons
dropdown triggers
icons


4.isDisplayed() Command in Selenium
⚑ Method Signature
boolean isDisplayed()
πŸ‘‰ isDisplayed() checks whether a web element is visible on the webpage or not.
It returns:
true β†’ element is visible
false β†’ element is not visible
🧩 Correct Definition

isDisplayed() is a WebElement method used to determine whether an element is present in the DOM and visible on the web page (i.e., not hidden by CSS or not outside the viewport). It returns a boolean value.

⚑ Example
WebElement logo = driver.findElement(By.id("logo"));

boolean status = logo.isDisplayed();
System.out.println(status);
πŸ” What it actually checks

isDisplayed() verifies:

βœ” Element exists in DOM
βœ” Element is visible (not hidden using CSS like display: none)
βœ” Element has non-zero size
βœ” Element is not collapsed or invisible

5.isEnabled() Command in Selenium
🧾 Method Signature
boolean isEnabled()
🧠 Meaning
πŸ‘‰ isEnabled() checks whether a web element is enabled or disabled for interaction.
It returns:
true β†’ element is enabled (can be used)
false β†’ element is disabled (cannot be used)
🧩 Interview-ready Definition

isEnabled() is a WebElement method used to verify whether an element is in an active state and can be interacted with (like clicking or typing). It returns a boolean value.

⚑ Example
WebElement button = driver.findElement(By.id("login"));

boolean status = button.isEnabled();
System.out.println(status);
πŸ” What it actually checks

isEnabled() verifies:

βœ” Element is not disabled via HTML attribute (disabled)
βœ” Element is interactive
βœ” User actions like click/type are allowed
6.isSelected() Command in Selenium
🧾 Method Signature
boolean isSelected()
🧠 Meaning
πŸ‘‰ isSelected() is used to check whether a checkbox or radio button is selected (checked) or not.
It returns:
true β†’ element is selected (checked)
false β†’ element is not selected
🧩** Interview-ready Definition**
isSelected() is a WebElement method used to determine whether a checkbox, radio button, or option in a select list is currently selected. It returns a boolean value.

Top comments (0)