Selenium Locators: A Complete Guide to All Types of Locators with Code Examples
Selenium is a popular open-source test automation framework used for automating web applications.
One of the key features of Selenium is its ability to interact with web elements on a page using different types of locators.
Locators are used to identify web elements on a page, such as buttons, text boxes, links, etc., and perform actions on them.
In this article, we will discuss the different types of locators in Selenium and how to use them in your test automation scripts.
The ID locator is the most reliable and preferred locator strategy in Selenium. It uses the "id" attribute of the HTML tag to locate web elements. Here's an example:
WebElement element = driver.findElement(By.id("element-id"));
Name LocatorThe Name locator strategy uses the "name" attribute of the HTML tag to locate web elements. Here's an example:
WebElement element = driver.findElement(By.name("element-name"));
Class Name LocatorThe Class Name locator strategy uses the "class" attribute of the HTML tag to locate web elements. Here's an example:
WebElement element = driver.findElement(By.className("element-class"));
Tag Name LocatorThe Tag Name locator strategy uses the HTML tag name to locate web elements. Here's an example:
WebElement element = driver.findElement(By.tagName("element-tag"));
Link Text LocatorThe Link Text locator strategy is used to locate links on a web page. It uses the text of the link to locate the element. Here's an example:
WebElement element = driver.findElement(By.linkText("element-link-text"));
Partial Link Text LocatorThe Partial Link Text locator strategy is used to locate links on a web page by matching a part of the link text. Here's an example:
WebElement element = driver.findElement(By.partialLinkText("partial-link-text"));
CSS Selector LocatorThe CSS Selector locator strategy uses CSS selectors to locate web elements. Here's an example:
WebElement element = driver.findElement(By.cssSelector("element-css-selector"));
XPath LocatorThe XPath locator strategy uses XPath expressions to locate web elements. Here's an example:
WebElement element = driver.findElement(By.xpath("element-xpath"));
Now that we've covered all the different types of locators in Selenium, let's look at a few best practices for using them.
Best Practices for Using Selenium Locators:
- Use the ID locator whenever possible as it is the most reliable and efficient locator strategy.
- Avoid using the Tag Name locator as it may not be unique on a web page.
- Use the CSS Selector or XPath locator strategies when you need to locate web elements based on their attributes or properties.
- Use the Link Text and Partial Link Text locator strategies to locate links on a web page.
- Use the Name and Class Name locator strategies only if there are no other unique locators available.
In conclusion, understanding the different types of locators in Selenium and how to use them is crucial for writing effective and efficient test automation scripts. By following the best practices outlined in this article, you can ensure that your Selenium locators are reliable and robust.
Read Next :
- Selenium WebDriver API
- Selenium WebDriver Setup
- Selenium Maven Dependancy
- Selenium Locators
- Selenium with Maven
- Selenium Page Object Model (POM)
- Selenium TestNG
- Selenium Handling Alerts
- Selenium Handling Frames and Windows
- Selenium Waits
- Selenium Cross-browser Testing
- Selenium Reporting
- Selenium Stale Element Exception
- TestNg Class Version Error
- Selenium IDE Download & Installation on Chrome
- Selenium IDE Download & Installation on Microsoft Edge
- Selenium IDE Download & Installation on Mozilla Firefox
- Selenium vs Cypress vs Puppeteer
- Selenium Interview Questions
Selenium Tutorials
- Setting up selenium IDE - Download and install selenium IDE on windows
- Selenium vs Cypress vs Puppeteer
- Selenium Interview Questions
- Selenium WebDriver API
- Selenium WebDriver Setup
- Selenium Maven Dependancy
- Selenium Locators
- Selenium with Maven
- Selenium Page Object Model (POM)
- Selenium TestNG
- Selenium Handling Alerts
- Selenium Handling Frames and Windows
- Selenium Waits
- Selenium Cross-browser Testing
- Selenium Reporting
- Selenium Stale Element Exception
- TestNg Class Version Error
- Selenium IDE Download & Installation on Chrome
- Selenium IDE Download & Installation on Microsoft Edge
- Selenium IDE Download & Installation on Mozilla Firefox
- Selenium vs Cypress vs Puppeteer
- Selenium Interview Questions
Cypress Tutorials
Cypress Archives
- Cypress Installation Errors
- How to Install Cypress
- Cypress Uncaught Exception Handling
- Cypress Automation Examples
- Cypress Automation Tool Interview Questions
- Cypress File Upload Examples
- Error 509 Bandwidth Exceeded
- Cypress Commands
- Cypress Custom Commands
- Handling Cypress Tokens & LocalStorage
- Handling Cypress Multitabs
- Cypress Parallelization
- Cypress waits
- Cypress Still Waiting Error
- Cypress Test Run Errors
- Cypress vs Selenium vs Puppeteer
- Cypress vs Selenium
- Cypress vs Puppeteer