- What are locators in Selenium?
- How to locate a web element in DOM?
- Different Types of Locators in Selenium
- Here’s an example of how to locate an element by XPath:
- Here’s an example of how to locate an element by partial link text:
- How to use Selenium locators to find web elements with Selenium Web Drivers?
- Best Practices For Using Locators In Selenium Web Drivers:
- Making a Move with ID Locator In Selenium WebDrivers
- Final Word
If you’re an automation engineer, you’ve probably heard of Selenium Web Drivers for automating web-based applications. And for every Selenium test script, locators in Selenium are the foundation. The rationale is simple: locators in Selenium WebDriver assist you in conducting the necessary interactions with DOM components.
But are you struggling with flaky test suites despite knowing all that?
The answer is that you are choosing the wrong locators in Selenium Web Drivers. As a result, they can break down your whole test suite and ultimately lead to automation failure in Selenium.
To help you solve this crisis, we’ll cover everything you need to know about locators in Selenium, including their types and examples, how to locate web elements using various methods, and best practices for locators in Selenium.
What are locators in Selenium?
Locators in Selenium are one of the most essential components of automated testing. Simply put, a locator is a unique identifier Selenium Web Drivers uses to locate a specific web element on a web page. When you automate a web application using Selenium, you must interact with web elements like buttons, links, checkboxes, and input fields.
Therefore, you must identify these web elements using unique Selenium locators to interact with them. A locator can be an ID, name, class name, CSS selector, or XPath expression. Using a locator, you can instruct Selenium to find a web element and perform an action on it, such as clicking a button, filling out a form, or selecting an option from a drop-down menu.
How to locate a web element in DOM?
Before diving into the types of locators in Selenium, let’s first understand how to locate a web element in the Document Object Model (DOM).
The DOM is a tree-like structure that shows the HTML structure of a web page. Every web element in the DOM has a unique address that Selenium can use to locate. Therefore, it would be best to use a locator to locate a web element in the DOM.
Different Types of Locators in Selenium
There are several types of locators in Selenium that you can use to locate web elements on a web page. Let’s discuss the different locators in selenium:
- ID Locator: It locates a web element based on its ID attribute. This locator is generally considered the fastest and most reliable, as ID attributes are supposed to be unique on a web page.
- Class Locator: It finds a web element based on its class attribute. This locator is helpful when multiple elements on a web page share the same class attribute.
- Name Locator: It helps in locating a web element based on its name attribute. This locator is helpful when interacting with form elements like text boxes, radio buttons, and checkboxes.
- XPath Locator: It searches and navigates to the web element based on its position within the DOM tree or its attribute values. XPath locators are robust but can be slower and more brittle than other types of Locators in Selenium.
- CSS Locator: The CSS locator is used to locate web elements based on their CSS selector. The CSS selector is a pattern that is used to select web elements based on their attributes, such as ID, class, and name.
- Tag Name Locator: Based on its HTML tag name, elements are located. This locator is helpful when you must interact with all elements of a specific type, such as all links or all buttons.
- Link Text Locator: Based on the text of a hyperlink, a link text locator finds a web element in the DOM. This locator is helpful when interacting with a specific link on a web page.
- Partial Link Text Locator: Uses a partial match of the text of a hyperlink to find a web element in the DOM. This locator is helpful when interacting with a link with a changing or dynamic text value.
To use a Selenium locator, you first need to create an instance of the WebDriver class, representing the browser you want to automate. Once you have a WebDriver instance, you can use the findElement() or findElements() method to locate web elements using the locator of your choice.
Here’s an example of using the ID locator to find a web element with the ID “search box”:
// Create a WebDriver instance WebDriver driver = new ChromeDriver(); // Navigate to the web page you want to test driver.get(“https://www.example.com”); // Find the search box using the ID locator WebElement searchBox = driver.findElement(By.id(“search-box”)); // Interact with the search box searchBox.sendKeys(“Selenium locators”); searchBox.submit(); |
In the next sections, we’ll cover each locator in detail, along with examples of how to use them to locate web elements.
Locate Elements by ID
The ID locator is one of the most commonly used Selenium locators. It is a unique identifier that is assigned to a web element on a web page.
You can use the By.id() method in Selenium to locate an element by ID. Here’s an example of how to locate an element by ID:
WebElement element = driver.findElement(By.id(“element-id”)); |
Locate Elements by Name
You can use the By.name() method in Selenium to locate an element by name.
Here’s an example of how to locate an element by name:
WebElement element = driver.findElement(By.name(“element-name”)); |
Locate Elements by Class Name
The class name is assigned to a web element through HTML’s “class” attribute.You can use the By.className() method in Selenium to locate an element by class name.
Here’s an example of how to locate an element by class name:
WebElement element = driver.findElement(By.className(“element-class”)); |
Locate Elements by CSS Selector
What makes it powerful is that it allows you to select web elements based on complex patterns. You can use the By.cssSelector() method in Selenium to locate an element by CSS selector.
Here’s an example of how to locate an element by CSS selector:
WebElement element = driver.findElement(By.cssSelector(“#element-id .element-class”)); |
Locate Elements by XPath
The XPath locator is used to locate web elements based on their XPath expression. To locate an element by XPath, you can use the By.xpath() method in Selenium.
Here’s an example of how to locate an element by XPath:
WebElement element = |
driver.findElement(By.xpath(“//input[@name=’element-name’]”)); |
Locate Elements by Tag Name
In this locator, the tag name is the name of the HTML tag that is used to define the web element. You can use the By.tagName() method in Selenium to locate an element by tag name.
Here’s an example of how to locate an element by tag name:
WebElement element = driver.findElement(By.tagName(“input”)); |
Locate Elements by Link Text
You can use the By.linkText() method in Selenium to locate an element by link text.
Here’s an example of how to locate an element by link text:
WebElement element = driver.findElement(By.linkText(“Click Here”)); |
Locate Elements by Partial Link Text
The partial link text locator is used to locate web elements based on the partial link text. You can use the By.partialLinkText() method in Selenium to locate an element by partial link text.
Here’s an example of how to locate an element by partial link text:
WebElement element = driver.findElement(By.partialLinkText(“Here”)); |
Locate Multiple Elements
You can use the findElements() method instead of the findElement() method in Selenium to locate multiple elements. The findElements() method returns a list of web elements that match the specified locator.
Here’s an example of how to locate multiple elements by class name:
List<WebElement> elements = driver.findElements(By.className(“element-class”)); |
Similarly, we can use other types of locators to locate web elements on a web page.
To sum up it up, here’s a table that shows different types of locators and their corresponding methods in Selenium Web Drivers:
Locator Type | Method Name |
ID | find_element_by_id |
Name | find_element_by_name |
Class Name | find_element_by_class_name |
Tag Name | find_element_by_tag_name |
Link Text | find_element_by_link_text |
Partial Link Text | find_element_by_partial_link_text |
CSS Selector | find_element_by_css_selector |
XPath | find_element_by_xpath |
How to use Selenium locators to find web elements with Selenium Web Drivers?
To find a web element using a locator in Selenium Web Drivers, you first need to create an instance of the WebDriver. You can create an instance of the WebDriver using any programming language supported by Selenium, such as Java, Python, C#, and Ruby.
Once you have created an instance of the WebDriver, you can use the findElement() or findElements() method to locate web elements on a web page.
Here’s an example of how to use locators to find web elements with Selenium:
// create an instance of the WebDriver
WebDriver driver = new ChromeDriver();
// navigate to a web page
driver.get(“https://www.example.com”);
// locate an element by ID
WebElement elementById = driver.findElement(By.id(“element-id”));
// locate an element by name
WebElement elementByName = driver.findElement(By.name(“element-name”));
// locate an element by class name
WebElement elementByClass = driver.findElement(By.className(“element-class”));
//locate an element by CSS selector
WebElement elementByCss = driver.findElement(By.cssSelector(“#element-id.element -class”));
// locate an element by XPath
WebElement elementByXpath = driver.findElement(By.xpath(“//input[ @name=’element- name’]”));
// locate an element by tag name
WebElement elementByTag = driver.findElement(By.tagName(“input”));
// locate an element by link text
WebElement elementByLink = driver.findElement(By.linkText(“Click Here”));
// locate an element by partial link text
WebElement elementByPartialLink = driver.findElement(By.partialLinkText(“Here”));
// locate multiple elements by class name
List<WebElement> elementsByClass = driver.findElements(By.className(“element-class”));
// close the browser window
driver.quit();
Best Practices For Using Locators In Selenium Web Drivers:
Selenium Locators are crucial for scalable, maintainable, and reliable automated testing. The locators used to identify components on a web page can directly impact the usability and effectiveness of the automated testing process.
- Use an ID locator whenever possible: The ID locator is the fastest and most reliable locator in Selenium. Using the ID locator to locate a web element with an ID attribute would be best.
- Use CSS selectors for complex patterns: If you need to locate a web element based on a complex pattern, use the CSS selector locator. It allows you to select web elements based on their attributes, such as ID, class, and name.
- Avoid using XPath: Although XPath is a powerful locator, it is slower than other locators, such as ID and CSS selectors. If you can locate a web element using ID or CSS selector, avoid using XPath.
- Use unique attributes: If a web element does not have an ID attribute, you should use other unique attributes, such as name, class, or tag name, to locate it.
- Use explicit waits: When locating web elements in Selenium, you should use explicit waits to ensure that the web element is present and visible before interacting with it.
Making a Move with ID Locator In Selenium WebDrivers
The ID locator would be best to locate a web element with an ID attribute. Here are some tips for using the ID locator in Selenium:
- Use the findElement() method to locate a single web element by ID.
- Use the findElements() method to locate multiple web elements by ID.
- Use the wait() method to ensure the web element is present and visible before interacting with it.
- Use the getText() method to get the text value of a web element.
- Use the click() method to click on a web element.
- Use the sendKeys() method to enter text into an input field.
- Use the isSelected() method to check if a checkbox or radio button is selected.
- Use the isEnabled() method to check if a web element is enabled.
Final Word
It is clear to us that locators are essential for locating web elements in Selenium. Several types of locators are available in Selenium Web Drivers, such as ID, name, class name, CSS selector, XPath, tag name, link text, and partial link text.
When using locators in Selenium Web Drivers, you should follow best practices, such as using the ID locator whenever possible, avoiding XPath, and using explicit waits. By adhering to these best practices, you can create robust Selenium scripts for effective web automation.