#3.1 What XPath: Axes

#3.1 What XPath: Axes

XPath in Selenium: XPath in Selenium: Best Practices and Optimization

Table of contents

This table provides a detailed overview of XPath axes, their relationships, and how they can be combined. Use this as a reference to understand how to navigate XML/HTML document structures effectively.

AxisDescriptionRelationship to Context NodeInverse AxisNode TestExample
selfSelects the context nodeIs the context nodeselfAnyself::div
childSelects all children of the context nodeDirect childrenparentElement, Text, Comment, Processing Instructionchild::p
parentSelects the parent of the context nodeDirect parentchildElementparent::div
ancestorSelects all ancestors of the context nodeAll nodes above in hierarchydescendantElementancestor::section
ancestor-or-selfSelects the context node and all its ancestorsSelf and all nodes abovedescendant-or-selfElementancestor-or-self::div
descendantSelects all descendants of the context nodeAll nodes below in hierarchyancestorAnydescendant::li
descendant-or-selfSelects the context node and all its descendantsSelf and all nodes belowancestor-or-selfAnydescendant-or-self::p
following-siblingSelects all siblings after the context nodeSame level, afterpreceding-siblingElementfollowing-sibling::div
preceding-siblingSelects all siblings before the context nodeSame level, beforefollowing-siblingElementpreceding-sibling::h1
followingSelects everything after the closing tag of the context nodeAfter in document orderprecedingAnyfollowing::section
precedingSelects everything before the opening tag of the context nodeBefore in document orderfollowingAnypreceding::p
attributeSelects attributes of the context nodeAttributes of the nodeNoneAttributeattribute::href
namespaceSelects namespace nodes of the context nodeNamespace nodesNoneNamespacenamespace::*

Additional Information:

  1. Context Node: The node currently being processed or the node from which the path expression is anchored.

  2. Document Order: The hierarchical order of elements as they appear in the XML document, from top to bottom.

  3. Axis Specifiers: In XPath expressions, axis specifiers can be abbreviated:

    • child:: is the default axis (can be omitted)

    • @ is short for attribute::

    • // is short for /descendant-or-self::node()/

    • . is short for self::node()

    • .. is short for parent::node()

  4. Node Tests:

    • node(): Matches any node of any type

    • text(): Matches text nodes

    • comment(): Matches comment nodes

    • processing-instruction(): Matches processing instruction nodes

  5. Predicate Usage: Predicates (filters) can be applied to any axis, e.g., ancestor::div[1] selects the nearest ancestor that is a div.

  6. Axis Combinations: Axes can be combined in a single XPath expression, e.g., ancestor::div/following-sibling::p selects all p elements that are siblings of any ancestor div.

  7. Performance Considerations:

    • Forward axes (child, descendant, following, etc.) are generally faster than reverse axes (parent, ancestor, preceding, etc.).

    • descendant:: is usually slower than child:: because it searches deeper in the hierarchy.

  8. Special Cases:

    • The namespace axis is not supported in all XPath processors and is less commonly used.

    • The attribute axis cannot have children, descendants, or siblings.

This table and additional information provide a comprehensive overview of XPath axes and their relationships, suitable for both beginners and advanced users exploring XPath capabilities.