0
0
Selenium Javatesting~10 mins

XPath axes (parent, following-sibling, preceding) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies locating web elements using XPath axes: parent, following-sibling, and preceding-sibling. It checks that the correct elements are found and their text matches expected values.

Test Code - JUnit
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class XPathAxesTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/xpath-axes-demo");
    }

    @Test
    public void testXPathAxes() {
        // Locate child element
        WebElement child = driver.findElement(By.id("child1"));

        // Use parent axis to get parent element
        WebElement parent = child.findElement(By.xpath("parent::*"));
        assertEquals("parent-div", parent.getAttribute("id"));

        // Use following-sibling axis to get next sibling
        WebElement nextSibling = parent.findElement(By.xpath("following-sibling::*[1]"));
        assertEquals("sibling-div", nextSibling.getAttribute("id"));

        // Use preceding-sibling axis to get previous sibling of nextSibling
        WebElement preceding = nextSibling.findElement(By.xpath("preceding-sibling::*[1]"));
        assertEquals("parent-div", preceding.getAttribute("id"));
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window opens at https://example.com/xpath-axes-demo-PASS
2Find element with id 'child1'Page loaded with element <div id='child1'>Element with id 'child1' is foundPASS
3Find parent element of 'child1' using XPath 'parent::*'Parent element with id 'parent-div' is locatedParent element's id attribute equals 'parent-div'PASS
4Find following sibling of parent element using XPath 'following-sibling::*[1]'Following sibling element with id 'sibling-div' is locatedFollowing sibling's id attribute equals 'sibling-div'PASS
5Find preceding element of following sibling using XPath 'preceding-sibling::*[1]'Preceding element with id 'parent-div' is locatedPreceding element's id attribute equals 'parent-div'PASS
6Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: Element with the specified XPath axis is not found or attribute does not match expected value
Execution Trace Quiz - 3 Questions
Test your understanding
Which XPath axis is used to find the parent element of 'child1'?
Aparent::*
Bfollowing-sibling::*
Cpreceding::*
Dchild::*
Key Result
Using XPath axes like parent, following-sibling, and preceding-sibling helps navigate complex page structures efficiently. Always verify the XPath expressions against the actual DOM to avoid locating errors.