Selenium WebElement Commands

Learn via video courses
Topics Covered

Overview

Web application testing is frequently conducted using Selenium, a well-liked open-source automation testing framework. To conduct various operations on web elements, such as clicking buttons, entering data into fields, submitting forms, and validating web page elements, Selenium offers a large variety of tools and libraries. The WebElement in Selenium, which simulates an HTML element on a web page, is one of Selenium's essential parts. The Selenium WebElement idea, its syntax, and a set of frequently used WebElement in Selenium commands with examples are all covered in this article.

Introduction

Web applications are continually developing and evolving in the quick-paced world of web development nowadays. It is essential to thoroughly and effectively test web apps to guarantee their dependability and quality. Web testing tasks can be automated with the help of Selenium WebDriver, a popular open-source automation testing tool. It offers a wide range of features for interacting with web elements on a web page and supports several programming languages, including Java, C#, Python, etc.

The idea of WebElement in Selenium, which stands for an HTML element on a web page, is at the heart of Selenium WebDriver. Testing on web elements may be done programmatically thanks to WebElement, which serves as a proxy between the WebDriver script and the real web element. To interact with web elements, obtain element properties, start events, and more, WebElement in Selenium offers a rich collection of methods or commands. Building reliable and productive automated tests with Selenium WebDriver requires an understanding of and proficiency with certain WebElement commands.

What is a Selenium WebElement?

A Selenium WebElement is a representation of an HTML element on a web page that can be used to interact with the element using Selenium commands.

The syntax for finding a WebElement in Selenium is as follows:

In the above syntax, By.<locator_type> represents the type of locator used to locate the element, such as By.ID, By.NAME, By.CLASS_NAME, By.XPATH, etc. <locator_value> represents the value of the locator, which uniquely identifies the element on the web page.

List of Selenium WebElement Commands:

Selenium WebElement provides a wide range of commands to interact with web elements.

Here is a list of commonly used commands:

sendKeys() Command:

This command is used to enter text into a text field or a text area. It takes a variable number of arguments as input, which represent the text to be entered. It can be a single character, a string, or a combination of both.

Syntax:

Example Usage:

Explanation:

In this example, we first locate the username field using the By.id() locator strategy and store it in a WebElement object named usernameField. Then, we use the sendKeys() command to enter the text "john.doe" into the username field.

isDisplayed() Command:

This command is used to check if an element is displayed on the web page or not. It returns a boolean value, true if the element is displayed, and false otherwise.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement in a Selenium object named logoImage. Then, we use the isDisplayed() command to check if the logo image is displayed on the web page or not, and store the result in a boolean variable named isDisplayed.

isSelected() Command:

This command is used to check whether a checkbox, radio button, or option in a dropdown menu is selected. It returns a boolean value, true if the element is selected, and false otherwise.

Syntax:

Example Usage:

Explanation:

In this example, we locate the "Remember Me" checkbox element using the By.id() locator strategy and store it in a WebElement in a Selenium object named rememberMeCheckbox. Then, we use the isSelected() command to check whether the checkbox is selected, and store the result in a boolean variable named isSelected.

submit() Command:

This command is used to submit a form or a form element. It is typically used with a submit button or by pressing the Enter key on the keyboard when a form element is focused.

Syntax:

Example Usage:

Explanation:

In this example, we locate the search form element using the By.id() locator strategy and store it in a WebElement in a Selenium object named searchForm. Then, we use the submit() command to submit the form.

isEnabled() Command:

This command is used to check if an element is enabled or disabled on the web page. It returns a boolean value, true if the element is enabled, and false otherwise.

Syntax:

Example Usage:

Explanation:

In this example, we locate the submit button element using the By.id() locator strategy and store it in a WebElement in a Selenium object named submitButton. Then, we use the isEnabled() command to check if the submit button is enabled or not and store the result in a boolean variable named isEnabled.

getLocation() Command:

This command is used to get the location of an element on the web page. It returns a Point object that contains the x and y coordinates of the element.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement in a Selenium object named logoImage. Then, we use the getLocation() command to get the location of the logo image element and store the x and y coordinates in separate variables x and y.

clear() Command:

This command is used to clear the text entered in a text field or a text area.

Syntax:

Example Usage:

Explanation:

In this example, we locate the username field element using the By.id() locator strategy and store it in a WebElement in a Selenium object named usernameField. Then, we use the clear() command to clear the text entered in the username field.

getText() Command:

This command is used to get the visible text of an element. It returns a string containing the visible text of the element.

Syntax:

Example Usage:

Explanation:

In this example, we locate the page title element using the By.tagName() locator strategy and store it in a WebElement in a Selenium object named pageTitle. Then, we use the getText() command to get the visible text of the page title element and store it in a string variable named text.

getTagName() Command:

This command is used to get an element's HTML tag name. It returns a string containing the HTML tag name of the element.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement in a Selenium object named logoImage. Then, we use the getTagName() command to get the HTML tag name of the logo image element and store it in a string variable named tagName.

getCssValue() Command:

This command is used to get the value of a CSS property of an element. It takes a String argument propertyName that specifies the name of the CSS property whose value needs to be retrieved and returns a string containing the value of the specified CSS property.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement in a Selenium object named logoImage. Then, we use the getCssValue() command to get the value of the color CSS property of the logo image element and store it in a string variable named color.

getAttribute() Command:

This command is used to get the value of an attribute of an element. It takes a String argument attributeName that specifies the name of the attribute whose value needs to be retrieved and returns a string containing the value of the specified attribute.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement object named logoImage. Then, we use the getAttribute() command to get the value of the "src" attribute of the logo image element, which represents the URL of the image, and store it in a string variable named src.

click() Command:

This command is used to perform a click action on an element as if a user has clicked on the element with the mouse.

Syntax:

Example Usage:

Explanation:

In this example, we locate the submit button element using the By.id() locator strategy and store it in a WebElement object named submitButton. Then, we use the click() command to perform a click action on the submit button element.

getSize() Command:

This command is used to get the size of an element. It returns a Dimension object that contains the width and height of the element.

Syntax:

Example Usage:

Explanation:

In this example, we locate the logo image element using the By.xpath() locator strategy and store it in a WebElement object named logoImage. Then, we use the getSize() command to get the size of the logo image element and store the width and height in separate variables width and height.

Conclusion

  • Selenium WebElement is a representation of an HTML element on a web page.
  • WebElement in Selenium is a crucial component in Selenium WebDriver that represents web elements on a web page.
  • WebElement in Selenium provides a wide range of capabilities to interact with web elements and perform actions on them.
  • Understanding and effectively using WebElement commands can greatly enhance the automation testing capabilities of Selenium WebDriver.
  • These commands can be used to perform tasks such as entering text, checking for visibility or selection, submitting forms, retrieving element properties, triggering events, and more.