Skip to content
CodeKruPro

XPath Axes - Parent and Sibling

15/70

Video walkthrough

Watch on YouTube

XPath Axes

XPath axes let you navigate the DOM tree relative to an element:

  • parent:: - parent element
  • following-sibling:: - next sibling
  • preceding-sibling:: - previous sibling
  • ancestor:: - any ancestor
  • descendant:: - any descendant

On the Text Messages page, the bold text (id="boldText") has a <strong> child element.

HTML Structure:

<p id="boldText">
  <strong>A Bold Text</strong>
</p>

The <strong> is a child of the <p>. XPath axes let you traverse this relationship in both directions.

Task:

  1. Navigate to https://testkru.com/Elements/TextMessages
  2. Find the <strong> element inside #boldText using descendant XPath
  3. Print the strong element's text
  4. From the <strong> element, navigate to its parent and print the parent's ID

Expected output:

Strong text: A Bold Text
Parent ID: boldText
Read full tutorial on Codekru

About this lesson

XPath axes in Selenium Java: parent, ancestor, child and sibling navigation

Selenium Java AssignmentsBeginnerCSS and XPath Locators

How it works

An axis says which direction to move from the current element. child:: is the default (the plain /), descendant:: goes any depth down (// works much the same way), parent:: goes one level up (.. is its shorthand), ancestor:: goes any number of levels up, and following-sibling:: and preceding-sibling:: move sideways among elements that share a parent.

The name after the axis must match the element you land on. On Text Messages the strong sits inside a p, so parent::p finds it while parent::div finds nothing.

Axes are what make label-based locators possible. Each block on this page is a label div next to a content div, and //div[@id='boldTextLabel']/following-sibling::div/p goes from the label to the paragraph it describes.

About the assignment

The paragraph with id boldText has one child, a strong element. You will go down to that child with XPath, read its text, and then come back up to the paragraph, proving where you are by printing the paragraph's id.

What you will do

  1. Write an XPath that starts at the paragraph by its id and steps down to the strong element, either with / or with the descendant:: axis.
  2. Print the strong element's text after the label Strong text:.
  3. From the strong element, go to its parent. Either call findElement() on the element itself with a parent step, or write a full expression that ends in parent::p.
  4. Read the parent's id with getAttribute() and print it after the label Parent ID:.

Common mistakes

  • Starting a relative XPath with //

    Calling findElement() on an element with an expression that starts with // still searches the whole document. On this page it returns the sidebar's copyright paragraph, the first p in the document, not the paragraph around your element. Start with . (for example ./.. or .//span) to stay relative.

  • Naming the wrong tag after an axis

    parent::div from the strong matches nothing, because the parent is a p, so you get NoSuchElementException. Use parent::* when the tag does not matter.

  • Looking for siblings that are not there

    The strong is the only child of its paragraph, and the paragraph has no siblings of its own, so following-sibling::* from either one returns an empty list.

In a real test suite

Real pages often give the useful element no stable id but put one on something nearby: a form label, a table row header, or a card title. Axes let a test start from that anchor, for example "the Delete button in the row whose name cell says Alice".

This lesson ends the locator part of the course. From lesson 16 on you act on the elements you find, starting with click() on the Buttons page.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextMessages");
        
        // TODO: Find the <strong> element inside #boldText
        // TODO: Print its text
        // TODO: Navigate to parent and print parent's ID
        
        driver.quit();
    }
}
Read the full tutorial