Mastering Locators in Selenium WebDriver: The Ultimate Guide with Examples


Selenium is a popular open-source tool used for automating web browsers. It allows testers to write scripts in various programming languages such as Java, Python, C#, and Ruby to automate web applications. One of the critical aspects of Selenium automation is identifying web elements on a web page using locators. In this blog, we will discuss locators in Selenium and how to find them.

Locators are nothing but a way to locate web elements on a web page. Selenium WebDriver provides a set of locators that can be used to identify web elements on a web page. 

1. The various types of locators supported by Selenium WebDriver are:

1. ID Locator: This locator is the most common and efficient way to locate an element on a web page. Each web element on a page should have a unique ID. You can use the ID Locator to find an element using its ID.

2. Name Locator: The Name Locator is another way to locate an element using its name attribute. This is particularly useful when you want to locate multiple elements with the same name.

3. Class Name Locator: The Class Name Locator is used to locate elements using their class name attribute. This locator is useful when you want to locate multiple elements with the same class name.

4. Tag Name Locator: The Tag Name Locator is used to locate elements using their HTML tag name. This locator is useful when you want to locate a group of elements with the same tag name.

5. Link Text Locator: The Link Text Locator is used to locate anchor elements on a web page. This locator is useful when you want to locate links on a page.

6. Partial Link Text Locator: The Partial Link Text Locator is similar to the Link Text Locator, but it allows you to locate a link using only a part of its text.

7. CSS Selector Locator: The CSS Selector Locator is a powerful locator that allows you to locate elements using CSS selectors. This locator is useful when you want to locate elements based on their attributes or position on the page.

8. XPath Locator: The XPath Locator is another powerful locator that allows you to locate elements using XPath expressions. This locator is useful when you want to locate elements based on their position on the page, attributes, or text content.

2. How to Use Selenium Locators

Now that we have discussed the various types of locators in Selenium, let's see how to use them to find web elements on a page. Here's an example of how to find an element using the ID Locator:

1. ID Locator

The ID Locator is the most common and efficient way to locate an element on a web page. Each web element on a page should have a unique ID. You can use the ID Locator to find an element using its ID. Here is an example of how to find an element using the ID Locator:

bash
WebElement element = driver.findElement(By.id("element-id"));

  1. Name Locator

The Name Locator is another way to locate an element using its name attribute. This is particularly useful when you want to locate multiple elements with the same name. Here is an example of how to find an element using the Name Locator:

java
WebElement element = driver.findElement(By.name("element-name"));

  1. Class Name Locator

The Class Name Locator is used to locate elements using their class name attribute. This locator is useful when you want to locate multiple elements with the same class name. Here is an example of how to find an element using the Class Name Locator:

java
WebElement element = driver.findElement(By.className("element-class"));

  1. Tag Name Locator

The Tag Name Locator is used to locate elements using their HTML tag name. This locator is useful when you want to locate a group of elements with the same tag name. Here is an example of how to find an element using the Tag Name Locator:


java
List<WebElement> elements = driver.findElements(By.tagName("tag-name"));

  1. Link Text Locator

The Link Text Locator is used to locate anchor elements on a web page. This locator is useful when you want to locate links on a page. Here is an example of how to find an element using the Link Text Locator:

java
WebElement element = driver.findElement(By.linkText("link-text"));

  1. Partial Link Text Locator

The Partial Link Text Locator is similar to the Link Text Locator, but it allows you to locate a link using only a part of its text. Here is an example of how to find an element using the Partial Link Text Locator:

java
WebElement element = driver.findElement(By.partialLinkText("partial-link-text"));

  1. CSS Selector Locator

The CSS Selector Locator is a powerful locator that allows you to locate elements using CSS selectors. This locator is useful when you want to locate elements based on their attributes or position on the page. Here is an example of how to find an element using the CSS Selector Locator:

java
WebElement element = driver.findElement(By.cssSelector("css-selector"));


Inspecting CSS selectors is an essential skill for Selenium automation testing. It allows you to identify the location of elements on a web page and create CSS selector expressions to locate them.

Here are the steps to inspect CSS selectors using the Chrome Developer Tools:

  1. Open the Chrome Developer Tools by pressing F12 or Ctrl + Shift + I.

  2. Click on the Elements tab.

  3. Hover over the HTML element you want to inspect, and right-click on it.

  4. Click on Copy > Copy selector to copy the CSS selector expression of the element.

Alternatively, you can also use the Console tab to inspect CSS selectors:

  1. Open the Chrome Developer Tools by pressing F12 or Ctrl + Shift + I.

  2. Click on the Console tab.

  3. Type $$('CSS-selector-expression') in the console and press Enter. This will return an array of all elements that match the CSS selector expression.

  4. You can then use the console.dir() function to view the properties of the selected elements.

Here is an example of inspecting a CSS selector expression using the Chrome Developer Tools:



  1. XPath Locator

The XPath Locator is another powerful locator that allows you to locate elements using XPath expressions. This locator is useful when you want to locate elements based on their position on the page, attributes, or text content. Here is an example of how to find an element using the XPath Locator:

java
WebElement element = driver.findElement(By.xpath("xpath-expression"));


Let's study XPath in detail,

8.1. What is XPath?

XPath stands for XML Path Language, and it is a query language used for selecting nodes from an XML or HTML document. XPath is an essential tool for web scraping, data extraction, and automation testing with Selenium. It allows you to select specific elements on a web page using their attributes, text content, or position in the HTML tree structure.

XPath expressions can be used with various Selenium locators, including the findElement() and findElements() methods. XPath expressions are powerful, flexible, and can be used to locate elements in complex web applications. XPath expressions can also be used to create more advanced test scenarios, such as selecting multiple elements or selecting elements based on their parent or child nodes.


8.2. Types of XPath:

There are two types of XPath expressions: Absolute XPath and Relative XPath.

  1. Absolute XPath
An Absolute XPath expression starts with a single forward slash / and is used to locate an element from the root node of the HTML document. The Absolute XPath expression specifies the exact location of an element in the HTML tree structure, which means that if the HTML structure changes, the Absolute XPath expression may no longer be valid.

Here is an example of an Absolute XPath expression:

css
/html/body/div[2]/div[1]/form/input[1]
  1. Relative XPath
A Relative XPath expression does not start with a forward slash and is used to locate an element based on its relationship to other elements in the HTML document. Relative XPath expressions are more flexible than Absolute XPath expressions, and they are less likely to break if the HTML structure changes.

Here is an example of a Relative XPath expression:

css
//input[@name='search_query']

8.3. How to Inspect XPaths

Inspecting XPaths is an essential skill for Selenium automation testing. It allows you to identify the location of elements on a web page and create XPath expressions to locate them.

Here are the steps to inspect XPaths using the Chrome Developer Tools:

  1. Open the Chrome Developer Tools by pressing F12 or Ctrl + Shift + I.

  2. Click on the Elements tab.

  3. Hover over the HTML element you want to inspect and right-click on it.

  4. Click on Copy > Copy XPath to copy the XPath expression of the element.



Alternatively, you can also use the Console tab to inspect XPaths:

  1. Open the Chrome Developer Tools by pressing F12 or Ctrl + Shift + I.

  2. Click on the Console tab.

  3. Type $x('XPath-expression') in the console and press Enter. This will return an array of all elements that match the XPath expression.

  4. You can then use the console.dir() function to view the properties of the selected elements.

Here is an example of inspecting an XPath expression using the Chrome Developer Tools:

Now that we have seen how to use the various types of locators in Selenium, let's discuss some best practices when using locators.

3. Example Code for Selenium Locators

Here are some examples of how to find locators for elements on the Selenium practice form at https://www.techlistic.com/p/selenium-practice-form.html:

  1. Using ID Locator:
java
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Example
public static void main(String[] args)
WebDriver driver = new ChromeDriver(); driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); WebElement firstNameField = driver.findElement(By.id("firstName")); 
 } }
  1. Using Name Locator:
java
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Example
public static void main(String[] args)
WebDriver driver = new ChromeDriver(); driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); WebElement lastNameField = driver.findElement(By.name("lastName")); 
 } }
  1. Using Class Locator:
java
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Example
public static void main(String[] args)
WebDriver driver = new ChromeDriver(); driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); List<WebElement> genderButtons = driver.findElements(By.className("form-check-input")); 
 } }
  1. Using XPath Locator:
java
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Example
public static void main(String[] args)
WebDriver driver = new ChromeDriver(); driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); WebElement submitButton = driver.findElement(By.xpath("//button[@type='submit']")); 
 } }
  1. Using CSS Selector Locator:
java
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
public class Example
public static void main(String[] args)
WebDriver driver = new ChromeDriver(); driver.get("https://www.techlistic.com/p/selenium-practice-form.html"); WebElement stateDropdown = driver.findElement(By.cssSelector("select#state")); 
 } }

4. Best Practices when Using Locators

  1. Use ID Locators wherever possible: ID Locators are the most efficient way to locate an element on a web page. Each web element on a page should have a unique ID, and you should use the ID Locator to find the element.
  2. Avoid using XPath Locators: Although XPath Locators are powerful, they are also slower than other locators. If possible, try to use other locators such as ID or Name Locators.
  3. Use CSS Selector Locators for complex selections: If you need to select multiple elements or elements with complex attributes, the CSS Selector Locator is a powerful and efficient way to do so.
  4. Use Partial Link Text Locators for dynamic content: If the text content of a link changes dynamically, you can use the Partial Link Text Locator to locate the link.
  5. Use Relative XPath expressions: If you must use XPath Locators, try to use Relative XPath expressions instead of Absolute XPath expressions. Relative XPath expressions are faster and more robust than Absolute XPath expressions.

Conclusion

In conclusion, locators are an essential aspect of Selenium automation, and understanding how to use them correctly is crucial to writing robust and efficient test scripts. By using the best practices discussed in this blog, you can write maintainable and scalable Selenium automation scripts that can withstand changes in the web application under test.


What is Selenium WebDriver?  << Previous      ||      Next >>  Selenium WebDriver Commands

Author
Vaneesh Behl
Passionately writing and working in Tech Space for more than a decade.

Comments

Popular posts from this blog

14 Best Selenium Practice Exercises for Automation Practice

17 Best Demo Websites for Automation Testing Practice

Top Free YouTube Download Apps: Download YouTube Videos Software Free

Mastering Selenium Practice: Automating Web Tables with Demo Examples

What Role Graphic Design Services Play in Marketing

Java Date Format Validation with Regular Expressions: A Step-by-Step Guide

How to Automate Google Search with Selenium WebDriver

Automation Practice: Automate Amazon like E-Commerce Website with Selenium

Mastering User Interactions with the Actions Class in Selenium WebDriver

Automate GoDaddy.com Features with Selenium WebDriver