Table of contents
1.
Introduction
2.
WebElements Commands
2.1.
Example
3.
List of WebElement Commands
3.1.
sendKeys() command
3.1.1.
Syntax
3.1.2.
Example
3.2.
isDisplayed() command
3.2.1.
Syntax
3.2.2.
Example
3.3.
isSelected() command
3.3.1.
Syntax
3.3.2.
Example
3.4.
getLocation() command
3.4.1.
Syntax
3.4.2.
Example
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Web Elements Commands in Selenium

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web Elements are essentially HTML elements on any website. When we access these using Selenium, they are known as Selenium Web Elements and are found in all HTML documents consisting of HTML elements. Each of these elements is contained within a start tag and an end tag with the content in between. 

Once we are able to locate these web elements in the HTML document using one of the locating strategies we are able to do various verifications using the methods in Selenium WebElement Commands.

In this blog, we will learn about these WebElements and some of the commands associated with it.

WebElements Commands

Selenium WebElement Commands are basically methods that are applicable to most DOM elements on a webpage. Upon accessing these web elements they are represented via the WebElement Interface -  which is used by Selenium to interact with visible and invisible elements on the web page.

Just like WebDrivers, WebElements commands work in the same way, i.e. either the element being searched for is found or returned null/void.

Example

WebElement element = driver.findElement(By.id(“Password”));

Here the command returns either an element with id “Password” or null.

All actions on any WebElement will always be populated against any element, even though the action may not be valid on the element.

List of WebElement Commands

Now that we know what a WebElement Command is we will learn about some of the commands under it.

sendKeys() command

Sometimes we want some of the input fields to enter some content automatically while executing tests. Here, we can use the sendKeys() command to do the job after identifying the input web element using locating strategies like id, name, classname, etc.

Syntax

element.sendKeys("text");

The text here is supposed to be a CharSequence and this command works on text entry elements such as Input and Textarea.

Example

//Isolate the Web Element
WebElement inputElement = driver.findElement(By.id("username"));
//Use sendKeys to inject content
inputElement.sendKeys("User");

The above code can also be reduced to a single line

//Send value directly to the WebElement
driver.findElement(By.id("username")).sendKeys("User");

isDisplayed() command

When we need to verify whether a certain element is present and displayed we can use the isDisplayed() command. If the value is displayed it returns true, else the value returned is a NoSuchElementFound exception. 

Syntax

element.isDisplayed();

Example

//Isolate the WebElement
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
//We can shorten the above code as well
boolean status = driver.findElement(By.id("UserName")).isDisplayed();

isSelected() command

When our webpage contains input elements such as radio buttons, checkboxes, select options, and menu items it is essential to determine whether the elements are selected or not. The isSelected command does exactly that. It returns true if a specified element is selected, else it returns false.

Syntax

element.isSelected();

Example

WebElement element = driver.findElement(By.id("Gender"));
boolean status = element.isSelected();

We can shorten the above code as well

boolean status = driver.findElement(By.id("Gender")).isSelected();

getLocation() command

This command returns the location of any specified web element on the page. This does not require any parameter and returns a Point object as a result. The x and y coordinate thus can be obtained respectively.

Syntax

element.getLocation();

Example

WebElement element = driver.findElement(By.id("UserName"));
Point location = element.getLocation();
System.out.println("X coordinate: " + point.x + "Y coordinate: " + point.y);

Frequently Asked Questions

1. What is the NoSuchElementFound exception?

Ans: When we try to access an iterable beyond its maximum limit, Java throws a NoSuchElementFound exception.

 

2. What is a point object?

Ans: Point objects contain an x coordinate and a y coordinate and are used to store locations in integer precision.
 

3. Is WebElement an interface or a class?

Ans: WebElement is an interface however all the abstract methods are implemented using the RemoteWebElement class.
 

4. How is WebDriver different from WebElement?

Ans: WebDriver class includes methods that work on Broad Actions that are not element specific on the Webpage. However, WebElement works on specific elements and has methods regarding the same.

Conclusion

In this blog, we discussed WebElement Commands and some of its most popular methods.

You may want to learn more about JUnit Annotations in Selenium hereWe hope that this blog has helped you enhance your knowledge regarding render functions in the React testing library. Do upvote our blog to help other ninjas grow.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass