Advanced XPath Concepts



In our earlier tutorial, we got a basic Introduction to XPath. In this tutorial, let us try to explore some of the Advanced XPath Concepts which can help you find robust locators while writing your automation scripts.



XML Image

1. Axes

As introduced in earlier tutorial, Axes are used in XPath to locate the nodes based on their relationship with respect to another node. Here are the common axes keywords.

  • child: It selects the children node of the current node.
  • parent: It selects the parent node of the current node
  • ancestor: Using this keyword selects all the ancestors (i.e. all the parents, grandparents, etc) of the current node.
  • descendant: Using this keyword selects all the descendants (i.e. all the children, grandchildren, etc) of the current node.
  • following-sibling: This keyword selects all the sibling nodes following (i.e. just after) the current node.
  • preceding-sibling: This keyword selects all the sibling nodes preceding (i.e. just before) the current node.
  • attribute: This keyword is used to select the attribute of the current node or xml tag.


2. Operators

  • Union Operator:
    • The union operator is denoted by: |
    • Usage example: //li | //p
  • Logical Operator:

    There are 2 logical operators: and, or

  • Comparison Operator:

    The various comparison operators used in XPath are listed below:

    • =
    • !=
    • <
    • >
    • <=
    • >=


3. Functions

There are a lot of functions available in XPath, some of the functions commonly used by Test Automation Engineers are listed below:

  • text()
  • contains()
  • starts-with()
  • substring()
  • string-length()
  • last()
  • position()
  • count()

Previous Chapter Understanding XPath

Follow Me on Social Media

Advertisement
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount