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
- Write an XPath that starts at the paragraph by its
idand steps down to thestrongelement, either with/or with thedescendant::axis. - Print the strong element's text after the label
Strong text:. - 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 inparent::p. - Read the parent's
idwithgetAttribute()and print it after the labelParent 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 firstpin 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::divfrom thestrongmatches nothing, because the parent is ap, so you getNoSuchElementException. Useparent::*when the tag does not matter.Looking for siblings that are not there
The
strongis the only child of its paragraph, and the paragraph has no siblings of its own, sofollowing-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.
Related lessons
- XPath - BasicsThe XPath predicates that axes build on.
- XPath Functions - contains() and starts-with()Use
contains()andstarts-with()in the predicate of the element you start an axis from. - Get Element Text with getText()How
getText()handles text that sits in child elements like thisstrong.
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.
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();
}
}