Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
View Categories
  • Home
  • Tutorials
  • Selenium Automation Testing
  • Locators in Selenium

Locators in Selenium

Locators in Selenium

In Selenium, locators are used to identify and interact with elements on a web page (like buttons, text fields, dropdowns, etc.). Selenium WebDriver provides several types of locators to find elements efficiently.  

Types of Locators in Selenium

1. ID Locator

  • Finds an element by its id attribute (unique on a page).
📄
filename.js
# Syntax
driver.findElement(By.id("element-id"));

# html code
<input id="username" type="text">


# Example
WebElement username = driver.findElement(By.id("username"));

2. Name Locator

  • Finds elements by their name attribute (not always unique).
📄
filename.js
# Syntax
driver.findElement(By.name("element-name"));

# html code
<input name="email" type="text">

# Example
WebElement email = driver.findElement(By.name("email"));

3. CSS Selector

  • Uses XML path expressions to navigate the DOM.
📄
filename.js
# Syntax
driver.findElement(By.cssSelector("css-selector"));

# html code
#username            /* By ID */
.btn-submit          /* By Class */
input[name='email']  /* By Attribute */
div > p              /* Child Selector */

4. XPath Locator #

  • Uses XML path expressions to navigate the DOM.
📄
filename.js
# Syntax
driver.findElement(By.xpath("xpath-expression"));

# html code
//input[@id='username']       /* Finds by ID */
//*[contains(text(),'Login')] /* Partial text match */
//div[@class='container']/a   /* Child element */

5. Link Text

  • Link Text: Finds a link by its exact text.
📄
filename.js
# html code
<a href="/reset">Forgot Password?</a>

# Example for link Text
driver.findElement(By.linkText("Forgot Password?"));

6. Partial Link Text

  • Partial Link Text: Finds a link by partial text match.
📄
filename.js
# html code
<a href="/reset">Forgot Password?</a>

# Example for partia
l link text
driver.findElement(By.partialLinkText("Forgot"));

7. Class Name Locator

  • Finds elements by their class attribute (can match multiple elements).
📄
filename.js
# Syntax
driver.findElement(By.className("class-name"));

# html code
<button class="btn-submit">Submit</button>

# Example
WebElement submitBtn = driver.findElement(By.className("btn-submit"));

8. Tag Name Locator

  • Finds elements by their HTML tag (e.g., <div>, <a>, <input>).
📄
filename.js
# Syntax
driver.findElement(By.tagName("tag-name"));

# html code
<a href="/login">Login</a>

# Example
WebElement link = driver.findElement(By.tagName("a"));

Which Locator to Use?

LocatorWhen to Use
IDBest (if unique)
NameGood for forms
CSS SelectorFaster than XPath, flexible
XPathComplex traversals (parent/child)
Link TextOnly for hyperlinks
Class/TagLess reliable (often multiple matches)

Best Practices

  1. Prefer ID or Name (if available, as they are fastest).
  2. Use CSS Selector for better performance than XPath.
  3. Avoid XPath for simple cases (slower in older browsers).
  4. Use relative XPath (//) instead of absolute (/html/…).
  5. Avoid dynamic attributes (e.g., id=”button-1234″ changes frequently).

Locators are fundamental in Selenium automation, and choosing the right one improves script stability and performance. 

Selenium Automation Testing
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
Advantages and Disadvantages of Selenium WebDriverHow to launch the browsers ? 
Table of Contents
  • 4. XPath Locator
© 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105