Selenium Locators Explained 2024 | Master XPath, CSS, ID

 Locator Strategies in Selenium WebDriver


In Selenium WebDriver, locators are mechanisms used to locate and identify web elements on a web page so that you can interact with them or extract information from them. Accurately choosing the right locator strategy is crucial for writing robust and maintainable automation scripts. Selenium WebDriver supports various locator strategies, each with its own syntax and use cases.


Locators types


Here are the most commonly used locators in Selenium WebDriver:

1. ID Locator:

  • Syntax: By.id("idValue")
  • Use: Finds elements by their HTML id attribute, which should be unique on the page. This is one of the fastest and most reliable locator strategies when the id is unique.

2. Name Locator:

  • Syntax: By.name("nameValue")
  • Use: Locates elements by their HTML name attribute. It is not always unique, so be cautious when using it as multiple elements may share the same name.

3. Class Name Locator:

  • Syntax: By.className("className")
  • Use: Finds elements by their HTML class attribute. Be cautious, as multiple elements can share the same class name, which may require refining the selection further.

4. Tag Name Locator:

  • Syntax: By.tagName("tagName")
  • Use: Finds all elements with the specified HTML tag name. This locator is useful when you want to find elements of a particular type (e.g., all <input> elements).

5. Link Text Locator:

  • Syntax: By.linkText("linkText")
  • Use: Specifically used for locating anchor (<a>) elements by their visible link text. This strategy is helpful when the link text is unique on the page.

6. Partial Link Text Locator:

  • Syntax: By.partialLinkText("partialLinkText")
  • Use: Locates anchor elements by a partial match of their visible link text. This is useful when only part of the link text is known or is dynamic.

7. CSS Selector Locators:

CSS selectors are a lightweight and efficient way to locate elements based on their attributes, classes, and tag names. They are widely used in web development and are supported by all modern browsers.

  • Syntax: By.cssSelector("cssSelector")

  • CSS Selector Examples:

    • Tag Name Selector: Selects elements by their HTML tag name, e.g., input or a.
    • ID Selector: Selects elements by their id attribute, e.g., #username.
    • Class Selector: Selects elements by their class attribute, e.g., .btn-primary.
    • Attribute Selector: Selects elements by their attributes, e.g., [name='username'].
    • Combining Selectors: You can combine selectors to create more specific selections, e.g., input[name='username'].
  • Examples:

    • Locate an input element by its name attribute:
      By.cssSelector("input[name='username']")
    • Locate a link by its class:
      By.cssSelector(".btn-primary")

8. XPath Locators:

XPath (XML Path Language) is a language used for traversing XML documents, and it can also be used for HTML documents. In Selenium WebDriver, XPath expressions are used to locate elements. XPath locators offer extensive capabilities for navigating and selecting elements in the DOM.

  • Syntax: By.xpath("xpathExpression")

XPath Expressions:

  • Absolute XPath:
    Starts from the root of the document, such as /html/body/div[1]/form/input[2].
    Not recommended because it can be brittle and easily break if the page structure changes.

  • Relative XPath:
    Starts from a reference point, such as //input[@name='username'].
    Typically more robust as it's less dependent on the document's structure.

  • Attribute-Based Selection:
    To locate elements based on attributes, e.g., //input[@id='username'] or //a[@href='/logout'].

  • Text-Based Selection:
    To locate elements based on their visible text, e.g., //a[text()='Login'].

  • Using Axes:
    XPath allows you to navigate through the DOM hierarchy using axes like parent::, child::, following-sibling::, etc.

9. DOM Locator:

  • Syntax: By.domSelector("domSelector")
  • Use: This is a less common locator strategy that leverages JavaScript expressions to identify elements in the DOM. It can be powerful but is rarely used compared to XPath and CSS selectors.

10. By Chained Locators (ByChained):

  • Syntax: new ByChained(By.id("parent"), By.className("child"))
  • Use: This locator strategy allows you to chain multiple locators together. It helps when you want to locate an element that requires multiple criteria (e.g., locate a child element within a parent).

11. By All Locators (ByAll):

  • Syntax: new ByAll(By.id("elementId"), By.name("elementName"))
  • Use: Finds elements that match any of the locators provided in a sequence. Useful for locating elements with fallback options.

When to Use XPath vs. CSS Selector:

XPath:

  • Use XPath when you need to traverse the DOM both upwards and downwards.
  • Use XPath when you need more advanced and complex selections based on various criteria.
  • Be cautious with absolute XPath, as it can be brittle.

CSS Selector:

  • Use CSS selectors when your selection criteria are based on element attributes, classes, or tag names.
  • CSS selectors are often more concise and readable than complex XPath expressions.

Conclusion

In practice, the choice between XPath and CSS selectors depends on your specific needs and the structure of the web page. You can use both strategies effectively in your Selenium WebDriver scripts as needed. Understanding and mastering these locators is essential for writing robust, reliable, and maintainable automation scripts with Selenium WebDriver.

Post a Comment

0 Comments