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.
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.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window opens at https://example.com/xpath-axes-demo | — | PASS |
| 2 | Find element with id 'child1' | Page loaded with element <div id='child1'> | Element with id 'child1' is found | PASS |
| 3 | Find parent element of 'child1' using XPath 'parent::*' | Parent element with id 'parent-div' is located | Parent element's id attribute equals 'parent-div' | PASS |
| 4 | Find following sibling of parent element using XPath 'following-sibling::*[1]' | Following sibling element with id 'sibling-div' is located | Following sibling's id attribute equals 'sibling-div' | PASS |
| 5 | Find preceding element of following sibling using XPath 'preceding-sibling::*[1]' | Preceding element with id 'parent-div' is located | Preceding element's id attribute equals 'parent-div' | PASS |
| 6 | Test ends and browser closes | Browser window closes | — | PASS |
parent:: axis selects the direct parent node of the current element.following-sibling:: and it requires a node test like * for all siblings.following-sibling::* which correctly selects all following siblings. Others are incorrect syntax or wrong axis.<ul> <li id='item1'>First</li> <li id='item2'>Second</li> <li id='item3'>Third</li> </ul>
//li[@id='item1']/following-sibling::li[1]/text()li element with id 'item1', then selects its first following sibling li element, and gets its text.li after 'item1' is the li with id 'item2' which contains text 'Second'.//div[@class='menu']/preceding-sibling::div[2]preceding-sibling::div, the index applies to the node set returned by the axis, so parentheses are needed.(preceding-sibling::div)[2], not preceding-sibling::div[2].div of a span element that has a class 'highlight', but only if the parent div has a following sibling div with class 'footer'. Which XPath expression correctly achieves this?div of a span with class 'highlight', but only if that parent div has a following sibling div with class 'footer'.div of the span and filters it by checking if it has a following sibling div with class 'footer', which matches the requirement exactly.Pass the test and this pattern moves unknown