Locate Web elements – Selenium WebDriver Testing Free Training
Welcome to the Selenium WebDriver Testing Free Training series. In the last tutorial, we talked about the following things:
- Get webpage title
- Assert the webpage title
- Scenarios where we can call for assert title technique.
All right!! Let’s move ahead and talk about how to locate the web elements.
What is a Web element?
Before moving further, we should take a brief look at the web elements. A Web element represents an HTML element. That renders the data into an HTML page. A web page consists of different elements like buttons, input boxes, dropdown lists, links, password box, etc.
We recommend you get basic knowledge of HTML, and CSS to understand web elements and locators. You can refer following courses links:
Note: these courses are free.
To perform the actions on a web page (click on the button, provide the name to name text box, clear data of the text box) the first step is to find the element. To find the element, Selenium WebDriver provides the following methods:
- findElement() method for single web element.
- findElements() for more than one web elements.
These methods require the reference of the respective web element as a parameter and return org.openqa.selenium.WebElement interface object.
How can we get the reference of the respective element?
To get the reference of the web element, Selenium WebDriver has in-built org.openqa.selenium.By class. This class provides the mechanism to locate elements within a web page. Let’s talk about different static methods in detail:
This method locates the element by the ID attribute of the web element.
driver.findElement(By.id("identifierId")).sendKeys("Located by ID");
This method locates the element by the name attribute of the web element.
driver.findElement(By.name("identifier")).sendKeys("Located by NAME");
This method locates the element by tag name.
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
This method locates the element by class attribute of the web element name.
driver.findElement(By.className("whsOnd")).sendKeys("Located by className");
This method uses a CSS expression to locate the element.
driver.findElement(By.cssSelector("#identifierId")).sendKeys("Located by cssSelector ");
In our test automation codes, we generally prefer to use ID, name, className, etc. locators.
However, sometimes we could not locate any of them on the web page, and also sometimes locators of some elements change dynamically on the web page.
In these scenarios, we need to use an XPath locator. This locator is capable to locate complex and dynamically changing web elements.
We can find the location of any element on a web page using the XML path expressions.
- XPath is defined as an XML path.
- XML stands for the extensible markup language.
- It is used to locate web elements, especially when it’s not possible to find an element by name or ID, etc.
- Syntax: //tagname[@Attribute = ‘value’]
driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("Located by XPATH ");
When we have to locate the link by partial text, then here comes the partialLinkText to rescue.
Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:
For Instructor-led training
Master of Functional Automation Testing
For Self-Driven training
Automation with Selenium WebDriver (Java)
So, in this tutorial, we talked about web elements, methods to find the web element and By class and its techniques to locate web elements. We also looked at syntaxes to locate the web elements with different techniques.
In the next tutorial, we will talk about methods to perform different events or actions. We will also use the above locators to find the elements to perform the actions.
Happy learning, until then.