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)
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)