#2.0 What XPath: Basics

Souvik DeySouvik Dey
3 min read

What is XPath?

XPath (XML Path Language) is a query language designed to navigate through elements and attributes in an XML document. While originally created for XML, XPath has become an essential tool for locating elements in HTML documents, making it invaluable for web scraping and automated testing.

Key points about XPath:

  • XPath uses path expressions to select nodes or node-sets in an XML document.

  • It provides a powerful way to navigate hierarchical structures.

  • XPath expressions can use various criteria including element names, attributes, and their values.

Why use XPath in Selenium?

Selenium WebDriver supports multiple locator strategies, including ID, name, class name, and CSS selectors. However, XPath offers several advantages that make it a preferred choice in many scenarios:

  1. Flexibility: XPath can locate elements using a wide range of criteria, including text content, attribute values, and relative positions.

  2. Handling Dynamic Elements: For web applications with dynamically generated IDs or classes, XPath can use partial matches or relative paths to locate elements reliably.

  3. Text-based Selection: Unlike CSS selectors, XPath can easily select elements based on their text content, which is particularly useful for locating buttons or links with specific labels.

  4. Traversing the DOM: XPath provides powerful methods to navigate up, down, and across the Document Object Model (DOM) tree.

  5. Compound Conditions: XPath allows for complex locators that combine multiple conditions, making it possible to pinpoint elements with high precision.

Basic XPath Syntax and Structure

XPath uses path expressions to select nodes in a document. The expression is written as a series of steps, separated by forward slashes (/).

Basic Syntax:

//tag[@attribute='value']
  • //: Selects nodes anywhere in the document

  • tag: The name of the element

  • @attribute: The attribute to match

  • 'value': The expected value of the attribute

Key Components of XPath:

  1. Axes: Define the relationship between the current node and the nodes to be selected.

    • Example: ancestor::, descendant::, following-sibling::
  2. Node Tests: Specify the node type and name to be selected.

    • Example: div, @class, text()
  3. Predicates: Filter expressions enclosed in square brackets.

    • Example: [position() = 1], [@id='username']

Common XPath Expressions:

  1. Select all elements of a specific type:

     //input
    

    Selects all <input> elements in the document.

  2. Select element by ID:

     //div[@id='content']
    

    Selects the <div> element with id="content".

  3. Select element by class:

     //p[@class='highlight']
    

    Selects all <p> elements with class="highlight".

  4. Select element by text content:

     //button[text()='Submit']
    

    Selects the <button> element with the exact text "Submit".

  5. Select element by partial attribute value:

     //img[contains(@src, 'logo')]
    

    Selects all <img> elements where the src attribute contains "logo".

  6. Select element by position:

     (//ul[@class='menu']/li)[1]
    

    Selects the first <li> element within a <ul> with class="menu".

  7. Select element with multiple conditions:

     //input[@type='text' and @name='username']
    

    Selects <input> elements with both type="text" and name="username".

XPath Axes:

XPath axes allow you to select nodes based on their relationship to the current node.

  1. child: Selects children of the current node

     //div[@id='parent']/child::p
    
  2. descendant: Selects all descendants (children, grandchildren, etc.) of the current node

     //div[@id='container']//descendant::a
    
  3. parent: Selects the parent of the current node

     //input[@id='username']/parent::div
    
  4. ancestor: Selects all ancestors (parent, grandparent, etc.) of the current node

     //button[@id='submit']/ancestor::form
    
  5. following-sibling: Selects all siblings after the current node

     //label[@for='email']/following-sibling::input
    
  6. preceding-sibling: Selects all siblings before the current node

     //input[@type='submit']/preceding-sibling::input
    

Understanding these basic XPath concepts and syntax will provide a strong foundation for creating effective locators in Selenium WebDriver. As you progress, you'll learn to combine these basic elements to create more complex and precise XPath expressions.

0
Subscribe to my newsletter

Read articles from Souvik Dey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Souvik Dey
Souvik Dey

I design and develop programmatic solutions for Problem-Solving.