EP 1: Introduction to Selenium Locators

EP 1: Introduction to Selenium Locators

Selenium Introduction

Selenium is an open-source software framework primarily used for automating web applications. It provides a way to interact with web browsers and perform various actions programmatically, simulating user interactions. Selenium is widely used for web testing, but it can also be used for web scraping, automating repetitive tasks, and more.

What is Selenium Locators?

Selenium locators are mechanisms that allow you to identify and interact with elements on a web page when using Selenium WebDriver. They provide a way to target specific HTML elements, such as buttons, input fields, links, and more, so that you can automate interactions with these elements during testing or web automation.

Types of Selenium Locators

  1. ID Locator:
    • Syntax: By.id("elementId")
    • Usage: Selects the element with the specified "id" attribute.
  2. Name Locator:
    • Syntax: By.name("elementName")
    • Usage: Selects elements based on the "name" attribute.
  3. Class Name Locator:
    • Syntax: By.className("elementClassName")
    • Usage: Selects elements based on the "class" attribute.
  4. Tag Name Locator:
    • Syntax: By.tagName("tagName")
    • Usage: Selects elements based on their HTML tag name (e.g., "div", "input", "a").
  5. Link Text Locator:
    • Syntax: By.linkText("linkText")
    • Usage: Selects anchor (link) elements with the exact link text.
  6. Partial Link Text Locator:
    • Syntax: By.partialLinkText("partialLinkText")
    • Usage: Selects anchor (link) elements with link text containing the specified partial text.
  7. XPath Locator:
    • Syntax: By.xpath("xpathExpression")
    • Usage: Selects elements based on an XPath expression, which allows for complex and flexible element targeting.
  8. CSS Selector Locator:
    • Syntax: By.cssSelector("cssSelector")
    • Usage: Selects elements based on a CSS selector expression, which is another powerful way to target elements.

Example of Selenium Locators


Which locators to use

  1. ID Locator:
    • Use when the element has a unique "id" attribute, as IDs should be unique within the HTML document.
  2. Name Locator:
    • Use when the element has a "name" attribute that is unique or when you need to interact with multiple elements with the same name.
  3. Class Name Locator:
    • Use when the element has a specific "class" attribute that is unique or when you want to target multiple elements with the same class.
  4. Tag Name Locator:
    • Use when you want to target multiple elements of the same HTML tag type, such as targeting all "div" elements or all "a" (link) elements.
  5. Link Text Locator:
    • Use when you want to target anchor (link) elements with the exact link text.
  6. Partial Link Text Locator:
    • Use when the link text is long, and you only want to match part of it.
  7. XPath Locator:
    • Use when there is no other unique identifier available or when you need to navigate through complex DOM structures.
  8. CSS Selector Locator:
    • Use when you are familiar with CSS selectors and need to target elements based on CSS attributes.