WebElement in Selenium: How to Locate with Examples!

Last updated on Sep 10,2024 58.8K Views

WebElement in Selenium: How to Locate with Examples!

edureka.co

Elements are an essential part of something abstract, the same way Selenium Certification encapsulates a simple form element called WebElement which represents an HTML element. So, in this article, we will be digging deep in understanding the major role played by the WebElement in Selenium. 

So, let’s begin by understanding what are web elements.

What are WebElements in Selenium?

Anything that is present on the web page is a WebElement such as text box, button, etc. WebElement represents an HTML element. Selenium WebDriver encapsulates a simple form element as an object of the WebElement. It basically represents a DOM element and all the HTML documents are made up by these HTML elements. 


html
	body
		 My First Heading;
		 My first paragraph;
	/body
/html

There are various techniques using which the WebDriver identifies the WebElements which are based on different properties like ID, Name, Class, XPath, Tagname, CSS Selectors, link Text, etc.

WebDriver provides two methods to find the elements on the web page.

Types of Web elements Selenium

WebElements in Selenium can be divided into different types, namely:

  1. Edit box
  2. Link
  3. Button
  4. Image, image link, an image button 
  5. Text area
  6. Checkbox
  7. Radio button
  8. Dropdown list

This is about the different types of WebElements in Selenium.

Now let’s understand the operations to be performed on them.

Operations performed on the Selenium WebElements

In order to access WebElements, we need to perform a set of operations starting with browser actions until the operations are performed on  frames.

These are the operations that are performed on the WebElement.

Now, the question is how to locate these WebElements to perform the above actions.

How to locate web elements on the web page

Selenium provides functionalities that help in locating the element on a web page. It makes use of element locators. Let’s take a look at how locators help in finding the WebElement. 
Locators are defined as an address that identifies a WebElement uniquely within a web page. It is a command that tells Selenium IDE which GUI elements it needs to operate, like – Text Box, Buttons, Check Boxes, etc. Using the right locator ensures that the tests are faster, more reliable and has lower maintenance over releases.

Types of element locators

In order to identify WebElements accurately and precisely, Selenium makes use of different types of locators, namely:

Also, check out this video where our Selenium expert explains how to locate elements using element locators by demonstrating a simple demo

Locators In Selenium Webdriver | Elements Locators In Selenium | Selenium Training | Edureka

This Edureka video on Locators in Selenium talks about different types of selenium locators and steps involved to locate a web element using locators along with examples.

Having understood how to locate the web elements, let’s move on and work on a demo that demonstrates the operations performed on the web element.

Example to Locate WebElement in Selenium

To start with the demo, we require certain prerequisites to work on WebElements in Selenium.

In this demo, we’ll see how to automate one of the most famous e-commerce website Amazon where we’ll understand the WebElement interface.

Take a look at this video where our Selenium expert is explaining how WebElement plays an important role in testing an application

WebElement in Selenium | Web Elements & Element Locators | Selenium Certification | Edureka

This Edureka ‘Web elements in Selenium’ video helps you understand how web element plays a major role in testing an application.

package co.edureka;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumWebElement {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopChromechromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.in/");
driver.manage().window().maximize();
Thread.sleep(4000);
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Poco F1");
Thread.sleep(4000);
driver.findElement(By.className("nav-input")).click();
driver.findElement(By.linkText("ACM")).click();
driver.navigate().to("http://edureka.co/blog");
Thread.sleep(4000);
driver.navigate().back();
driver.quit();

}

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh
With this, we come to an end to this “WebElement in Selenium” blog. I hope you guys enjoyed this article and understood how web elements play a major role while working on Selenium. Now that you have understood why web elements are important check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software.
Got a question for us? Please mention it in the comments section of “WebElement in Selenium,” and we will get back to you.
Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 28th October,2024

28th October

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 23rd November,2024

23rd November

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Unlocking the Secrets of Automated Testing With Selenium Framework