Selenium Locators

In Python Selenium, locators are used to find and interact with elements on a web page. These locators help Selenium identify web elements like buttons, text fields, links, etc., allowing you to perform actions like clicking, entering text, or extracting information.

Selenium offers several types of locators, each with its specific use case depending on how the element is defined in the HTML. Below are the most common Selenium locators and how to use each.

1. ID Locator

  • Syntax: driver.find_element(By.ID, "element_id")
  • Use: When the HTML element has a unique id attribute.
  • Example:
    element = driver.find_element(By.ID, "username") element.send_keys("user123")

  • Best Use Case: When an element has a unique ID, it is often the most reliable and fastest locator to use.

2. Name Locator

  • Syntax: driver.find_element(By.NAME, "element_name")

  • Use: For locating elements use the name attribute in HTML.

  • Example:
    element = driver.find_element(By.NAME, "email") element.send_keys("example@example.com")

  • Best Use Case: Useful when form elements (like input fields) have unique name attributes.

3. Class Name Locator

  • Syntax: driver.find_element(By.CLASS_NAME, "class_name")

  • Use: To find an element based on the class attribute in the HTML.

  • Example:
    element = driver.find_element(By.CLASS_NAME, "submit-button") element.click()

  • Best Use Case: When you want to locate elements that share the same class, like groups of buttons or text fields. However, it may not be ideal if multiple elements share the same class name.

4. Tag Name Locator

  • Syntax: driver.find_element(By.TAG_NAME, "tag_name")

  • Use: For selecting elements by their HTML tag (like div, a, button, etc.).

  • Example:
    element = driver.find_element(By.TAG_NAME, "button") element.click()

  • Best Use Case: Primarily used when you want to retrieve elements of a specific tag, e.g., collecting all a (link) tags on a page.

5. Link Text Locator

  • Syntax: driver.find_element(By.LINK_TEXT, "link_text")
  • Use: Finds a link (anchor tag) by its exact text.
  • Example:
    element = driver.find_element(By.LINK_TEXT, "Click here") element.click()

  • Best Use Case: When you’re searching for a hyperlink and you know the exact visible text of the link. This only works for a (anchor) tags.

6. Partial Link Text Locator

  • Syntax:
    driver.find_element(By.PARTIAL_LINK_TEXT, "partial_link_text")

  • Use: Locates a link by matching a portion of the text contained within it.

  • Example:
    element = driver.find_element(By.PARTIAL_LINK_TEXT, "Click") element.click()

  • Best Use Case: When the full link text is long or dynamic, and you want to match just a part of it. Like in cases where only part of the link text is known.

7. CSS Selector Locator

  • Syntax:
    driver.find_element(By.CSS_SELECTOR, "css_selector")

  • Use: Uses CSS selectors to locate elements. CSS selectors are very powerful and can locate elements by their ID, class, attributes, or structure within the HTML.

  • Example:
    element = driver.find_element(By.CSS_SELECTOR, "input[type='text']") element.send_keys("sample text")

  • Best Use Case: CSS selectors are highly versatile and can be used for complex locators. They are generally faster than XPath and allow targeting more specific elements.

Some common CSS selector examples:

  • By class: "input.submit"
  • By ID: "#submit-button"
  • By attribute: "input[name='username']"

8. XPath Locator

  • Syntax: driver.find_element(By.XPATH, "xpath_expression")

  • Use: XPath allows you to locate elements via their hierarchical position in the HTML DOM. It’s very powerful but also the most complex locator type.

  • Example:
    element = driver.find_element(By.XPATH, "//input[@id='username']") element.send_keys("user123")

  • Best Use Case: Useful for locating elements when no other locator (ID, name, class, etc.) is unique enough. XPath can navigate through parent, child, and sibling elements.

Some XPath examples:

  • By attribute: "//input[@id='username']"
  • By position: "//div[1]/input[2]"
  • Text-based: "//a[text()='Login']"

9. Custom Attribute Locators (with CSS or XPath)

  • CSS Example:
    driver.find_element(By.CSS_SELECTOR, "[attribute='value']")

  • XPath Example:
    driver.find_element(By.XPATH, "//*[@attribute='value']")

  • Use: For elements that have custom attributes in the HTML.

  • Example:
    element = driver.find_element(By.CSS_SELECTOR, "button[data-test='submit']") element.click()

  • Best Use Case: Useful when elements have custom data attributes, e.g., data-* attributes.

Summary of Locator Use Cases:

  • ID: Fast and reliable if the element has a unique id.

  • Name: Ideal for form elements with unique name attributes.

  • Class Name: Great when classes are unique or used on a small number of elements.

  • Tag Name: Useful for locating elements by their tag type.

  • Link Text/Partial Link Text: Best for finding anchor elements with specific text.

  • CSS Selector: Versatile and often the fastest way to locate complex elements.

  • XPath: Powerful for locating elements that are deeply nested or don’t have unique attributes.