Maximize your web testing efficiency with WebElement – the go-to tool for interacting with elements on a web page. Selenium’s WebElement offers a user-friendly interface with a limited number of methods, making it simple to use in all scripts. In this article, Novatesting would like to list and describe the basic functions of WebElement.
I. Understanding WebElement
WebElement is a critical tool for web testing, allowing testers to interact with elements on a web page. It is the go-to tool for web testing, making the process much more efficient. In Selenium, WebElement offers a user-friendly interface with a limited number of methods, making it simple to use in all scripts.
II. Basic Functions of Selenium’s WebElement

III. Locator
A locator is a string of characters that specifies the location of a WebElement on a web page. It is used to identify the element and interact with it using Selenium WebDriver. There are several ways to locate elements in Selenium, including:
- ID: The most efficient way to locate an element is by its ID, as it is unique to a single element on the page.
- Name: The name attribute can also be used to locate elements, although it is not always unique.
- Class Name: This locator looks for elements with a specific class name.
- Tag Name: This locator looks for elements with a specific HTML tag, such as “input” or “a”.
- Link Text: This locator looks for anchor elements with a specific link text.
- Partial Link Text: This locator looks for anchor elements with a partial match of the link text.
- CSS Selector: This locator uses a CSS selector expression to locate elements.
- Xpath: This locator uses an Xpath expression to locate elements.
In Selenium WebDriver, the findElement
method is used to locate elements on a web page, and it accepts a locator as an argument. It returns the first WebElement that matches the specified locator. To interact with the element, you can use the methods available for the WebElement class, such as click(), sendKeys(), etc.
// ID Locator
WebElement element = driver.findElement(By.id(“elementId”));
// Name Locator
WebElement element = driver.findElement(By.name(“elementName”));
// Class Name Locator
WebElement element = driver.findElement(By.className(“elementClass”));
// Tag Name Locator
WebElement element = driver.findElement(By.tagName(“elementTag”));
// Link Text Locator
WebElement element = driver.findElement(By.linkText(“Link Text”));
// Partial Link Text Locator
WebElement element = driver.findElement(By.partialLinkText(“Partial Link Text”));
// CSS Selector Locator
WebElement element = driver.findElement(By.cssSelector(“elementCssSelector”));
// Xpath Locator
WebElement element = driver.findElement(By.xpath(“//elementXpath”));
IV. In-depth of Basic functions of WebElement
clear()
: This method is used to clear the text from a text input element.
python
WebElement element = driver.findElement(By.id("elementId"));
element.clear();
click()
: This method is used to simulate a click event on the element.
python
WebElement element = driver.findElement(By.id("elementId"));
element.click();
findElement(By by)
: This method is used to find a sub-element within the current element.
Java
WebElement element = driver.findElement(By.id("elementId"));
WebElement subElement = element.findElement(By.id("subElementId"));
getAttribute(String name)
: This method is used to retrieve the value of a specified attribute of the element.
java
WebElement element = driver.findElement(By.id("elementId"));
String attributeValue = element.getAttribute("attributeName");
getCssValue(String propertyName)
: This method is used to retrieve the value of a specified CSS property of the element.
java
WebElement element = driver.findElement(By.id("elementId"));
String cssValue = element.getCssValue("propertyName");
getLocation()
: This method is used to retrieve the location of the element on the page as aPoint
object.
java
WebElement element = driver.findElement(By.id("elementId"));
Point location = element.getLocation();
getTagName()
: This method is used to retrieve the tag name of the element.
java
WebElement element = driver.findElement(By.id("elementId"));
String tagName = element.getTagName();
getText()
: This method is used to retrieve the text content of the element.
Java
WebElement element = driver.findElement(By.id("elementId"));
String text = element.getText();
isDisplayed()
: This method is used to determine whether the element is currently displayed on the page.
java
WebElement element = driver.findElement(By.id("elementId"));
boolean isDisplayed = element.isDisplayed();
isEnabled()
: This method is used to determine whether the element is currently enabled for user interaction.
java
WebElement element = driver.findElement(By.id("elementId"));
boolean isEnabled = element.isEnabled();
isSelected()
: This method is used to determine whether the element is currently selected.
java
WebElement element = driver.findElement(By.id("elementId"));
boolean isSelected = element.isSelected();
sendKeys(CharSequence... keysToSend)
: This method is used to send a sequence of keys to the element.
python
WebElement element = driver.findElement(By.id("elementId"));
element.sendKeys("text to be entered");
submit()
: This method is used to submit the form that the element is a part of.
python
WebElement element = driver.findElement(By.id("elementId"));
element.submit();
getSize()
: This method is used to retrieve the size of the element as aDimension
object.
java
WebElement element = driver.findElement(By.id("elementId"));
Dimension size = element.getSize();
getRect()
: This method is used to retrieve the size and location of the element as aRectangle
object.
java
WebElement element = driver.findElement(By.id("elementId"));
Rectangle rect = element.getRect();
getScreenshotAs(OutputType<X> target)
: This method is used to capture a screenshot of the element and return it in the specified format (e.g., PNG, BASE64, etc.).
java
WebElement element = driver.findElement(By.id("elementId"));
File screenshot = element.getScreenshotAs(OutputType.FILE);
In conclusion, the WebElement
class in Selenium provides a comprehensive interface for interacting with elements on a web page. From retrieving element properties, to performing actions such as clicking, submitting forms, and taking screenshots, WebElement
offers a user-friendly and flexible solution for automating web testing. By using the various methods and functions outlined above, testers can maximize their efficiency and ensure that their scripts accurately interact with the web page as intended. Whether you’re a seasoned Selenium user or just getting started, understanding and utilizing the WebElement
class is an essential step in becoming a successful web tester.