Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  •  Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  •  Locators in Selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • 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).
# 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).
# 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.
# 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.
# 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.
# 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.
# 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).
# 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>).
# 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 WebDriver
Table of Contents
  • 4. XPath Locator
© Copyright [2018-2025]. All Rights Reserved.