Understanding XPath in Selenium
XPath, a crucial Selenium technique, serves as a mechanism to traverse the HTML structure of a webpage. It operates as a syntax or language, facilitating the identification of elements on a webpage through XML path expressions.
In Selenium automation, there are instances where conventional locators like ID, name, or class fall short in locating elements. It is precisely in such scenarios that XPath proves invaluable for pinpointing these elusive elements on the webpage. Furthermore, XPath in Selenium demonstrates its versatility by being applicable to both XML and HTML documents.
Table of Contents: XPath in selenium
Syntax of XPath in Selenium:
XPath in Selenium is equipped with a robust set of functions and axes, empowering users to craft effective XPaths for web elements and establish distinctive locators. The syntax involves key elements that contribute to the versatility of XPath expressions:
XPath = //tagname[@Attribute=’Value’]
//
: Selects the current node.tag name
: Refers to the tag name of a specific node.@
: Allows the selection of attributes.attribute
: Denotes the attribute name of the node.value
: Represents the value of the node.
This syntax forms the foundation for constructing XPath expressions, offering flexibility and precision in identifying elements within a webpage.
Type of XPath Selection Methods
XPath in Selenium offers two primary methods for selecting elements:
1. Absolute XPath:
- Directly specifies the complete path to an element from the root of the HTML document.
- Drawback: Prone to failure if the structure of the webpage changes.
- Example:
/html/body/div[1]/div/div[2]/header/div/div[2]/a/img
2. Relative XPath:
- Begins its path from the middle of the HTML DOM structure, offering more flexibility.
- Starts with a double forward-slash (//), indicating the element can be located anywhere on the webpage.
- Allows writing shorter XPaths, reducing sensitivity to structural changes.
- Example:
//*[@id='block-perfecto-main-menu']/ul/li[6]/a
These two XPath approaches cater to different needs, with absolute XPath providing a precise but potentially brittle selection, while relative XPath offers adaptability and resilience to changes in the document structure. Choosing the right method depends on the specific requirements of element identification in Selenium automation.
Diverse XPath Locators for Element Identification
When locating elements on web pages, various XPath locators cater to different identification needs:
1. Tag Name:
- Targets elements based on their HTML tag.
- Example:
//div
2. ID:
- Locates elements using their unique identifier.
- Example:
//*[@id='elementId']
3. Class Name:
- Identifies elements based on their CSS class.
- Example:
//*[@class='className']
4. Name:
- Locates elements by their ‘name’ attribute.
- Example:
//*[@name='elementName']
5. Attribute Value:
- Targets elements using a specific attribute-value pair.
- Example:
//*[@attribute='value']
6. Contains():
- Used to find elements with attribute values containing a specified substring.
- Example:
//tag[contains(@attribute, 'substring')]
7. Text():
- Locates elements based on their visible text.
- Example:
//tag[text()='desiredText']
8. Starts-with():
- Identifies elements where the attribute value starts with a specified character or sequence.
- Example:
//tag[starts-with(@attribute, 'partialValue')]
These XPath locators offer a versatile toolkit for Selenium automation, allowing testers to choose the most appropriate method based on the specific attributes and characteristics of the elements they seek to locate.
Understanding XPath Axes in Web Element Navigation
In XPath, axes provide a way to navigate the hierarchical structure of XML or HTML documents. They define relationships between nodes, aiding in the identification of elements relative to the current node in the document tree. Here are some XPath axes commonly used in web element navigation:
1. Ancestor Axis (ancestor
):
- Selects all ancestors (parent, grandparent, etc.) of the current node.
- Example:
//div/ancestor::body
2. Descendant Axis (descendant
):
- Selects all descendants (children, grandchildren, etc.) of the current node.
- Example:
//body/descendant::div
3. Parent Axis (parent
):
- Selects the parent of the current node.
- Example:
//div/parent::body
4. Child Axis (child
):
- Selects all children of the current node.
- Example:
//body/child::div
5. Following Axis (following
):
- Selects all nodes appearing after the current node in the document.
- Example:
//div/following::span
6. Following-sibling Axis (following-sibling
):
- Selects all siblings appearing after the current node.
- Example:
//div/following-sibling::p
7. Preceding Axis (preceding
):
- Selects all nodes appearing before the current node in the document.
- Example:
//div/preceding::span
8. Preceding-sibling Axis (preceding-sibling
):
- Selects all siblings appearing before the current node.
- Example:
//div/preceding-sibling::p
XPath axes enhance the flexibility and precision of element selection, allowing testers to navigate the document tree efficiently based on relationships between different nodes.