The "path" in XPath describes a literal route through the document tree, starting from either the root or a specific context node.
XPath (XML Path Language) is a powerful query language used to navigate and select elements or attributes within XML and HTML documents. It treats a document as a tree of nodes, allowing users to define precise paths to extract data or locate elements for automation. Core Syntax and Concepts The "path" in XPath describes a literal route
: Everything in an XML/HTML document—elements, attributes, and text—is a node. Path Expressions : / : Selects from the root node (absolute path). Core Syntax and Concepts : Everything in an
// : Selects nodes anywhere in the document (relative path). . : Refers to the current node. .. : Refers to the parent of the current node. @ : Selects attributes (e.g., @class , @id ). Absolute vs. Relative XPath XPath Syntax - W3Schools
Choosing the right type of XPath is critical for maintaining stable automation scripts or web scrapers. Syntax Example Description /html/body/div[1]/input Defines the complete path from the root. Fragile : Breaks easily if the page structure changes. Relative //input[@id='email'] Starts from anywhere using a double slash // . Robust : More compact and less prone to breaking. Advanced Selection Techniques
When standard attributes like id are missing or dynamic, XPath provides functions and axes to locate complex elements. XPath Syntax - W3Schools