A Guide to WebElement in Selenium

A WebElement is used everywhere in selenium to perform various operations. But what is a WebElement? This post will talk about WebElement in detail.

A WebElement in Selenium is a Java interface representing a website’s HTML element. HTML element contains a start tag and an end tag with the content lying between those tags.

Syntax of a HTML element
<Tag> content </Tag>

HTML elements can be nested, as shown below –

<Tag1>
 <Tag2> 
   content
 </Tag2>
</Tag1>

So, let’s come back to the point that WebElement is an interface, not a class.

Declaration of the WebElement interface
public interface WebElement 
  extends SearchContext, TakesScreenshot

So, WebElement extends two more interfaces, SearchContext and TakesScreenshot.

SearchContext interface declares two methods that help find a single element or a list of elements depending on the situation. Below are the methods declared by the SearchContext interface –

  • List<WebElement> findElements(By by);
  • WebElement findElement(By by);

WebElement extends the SearchContext interface, so it ends up with the above methods.

TakesScreenshot interface declares only a single method that helps in taking the screenshot.

  • X getScreenshotAs(OutputType target) throws WebDriverException;

Other than the above methods, the WebElement interface also declares some methods –

  • void click()
  • void submit()
  • void sendKeys(CharSequence… keysToSend)
  • void clear()
  • String getTagName()
  • String getAttribute(String name)
  • boolean isSelected()
  • boolean isEnabled()
  • String getText()
  • List<WebElement> findElements(By by) [ WebElement also declares the findElements method which is specified by the SearchContext interface]
  • WebElement findElement(By by) [ WebElement also declares the findElement method, which is specified by the SearchContext interface]
  • boolean isDisplayed()
  • Point getLocation()
  • Dimension getSize()
  • Rectangle getRect()
  • String getCssValue(String propertyName)
WebElement interface methods
Now, the question is, if WebElement is only an interface, then which class is implementing its methods?

Many classes implement the WebElement interface, like RemoteWebElement, HtmlUnitWebElement, etc., and these classes provide the implementation of the methods declared by the WebElement interface.

We cannot make objects of an interface in Java. But, we can call the interface methods with the interface’s reference variable, which is what we do while calling the methods of the WebElement interface.

Reference variable of WebElement interface
      // Here webelement is the reference variable
     // of the WebElement interface
     webElement.clear();
     webElement.click();

Please read this section to understand the reference variables.

But the problem is still there. We cannot make objects of the WebElement as it’s an interface, and we need an object which provides the implementation of the methods declared by the WebElement interface.

This is where two widely used methods come into the picture.

  • List<WebElement> findElements(By by);
  • WebElement findElement(By by);

findElement method returns an object that implements the WebElement interface methods, whereas findElements returns a list of those objects.

So, whenever we write WebElement element = findElement(By.id(“firstName”)), it essentially is using the WebElement interface reference variable and calling the object’s implementation returned by the findElement method.

     WebElement element = findElement(By.id("firstName"))

     // Here webelement is the reference variable
     // of the WebElement interface
     webElement.clear();
     webElement.click();

A WebElement can be of any type. It can be a text, button, checkbox, or HTML element. The WebElement methods cannot be applied to each element type like we cannot clear a button, so we shouldn’t use the clear() method on a button element type. Using methods not supported by the element type may or may not result in an error.

List of WebElement methods in Selenium
Method Name What does the method do?
void clear()

This will clear the value if the element is a text entry element and has no effect on other elements.
void click()

It clicks on the element.
WebElement findElement(By by)

It finds the first element matching the locating criteria.
List<WebElement> findElements(By by)

It finds all the elements matching the locating criteria.
String getAttribute(String name)

It gets the value of the given attribute of the element
String getCssValue(String propertyName)

It gets the value of a given CSS property.
Point getLocation()

It returns a point containing the location of the top left-hand corner of the element.
Rectangle getRect()

It returns the location and size of the rendered element.

<X> X getScreenshotAs(OutputType<X> target)

It captures the screenshot and stores it in the specified location.
Dimension getSize()

It returns the size of the element on the page.
String getTagName()

It returns the tag name of this element.
String getText()

It returns the visible text of the element, including sub-elements.
boolean isDisplayed()

It tells whether the element is displayed or not.
boolean isEnabled()

It tells whether the element is currently enabled or not.
boolean isSelected()

It tells whether the element is selected or not.
void sendKeys(CharSequence… keysToSend)

It simulates typing into an element.
void submit()

If the current element is a form or an element within a form, then this will be submitted to the remote server.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.