Test Overview
This test verifies locating elements using XPath axes: parent, child, and sibling. It checks if the test can find a child element, then its parent, and a sibling element correctly on a sample webpage.
This test verifies locating elements using XPath axes: parent, child, and sibling. It checks if the test can find a child element, then its parent, and a sibling element correctly on a sample webpage.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestXPathAxes(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/xpath-axes-sample') def test_xpath_axes(self): driver = self.driver wait = WebDriverWait(driver, 10) # Find child element using child axis child = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@id='parent']/child::span[@class='child']"))) self.assertEqual(child.text, 'Child Element') # Find parent element using parent axis parent = child.find_element(By.XPATH, "parent::div") self.assertEqual(parent.get_attribute('id'), 'parent') # Find sibling element using following-sibling axis sibling = child.find_element(By.XPATH, "following-sibling::span[@class='sibling']") self.assertEqual(sibling.text, 'Sibling Element') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window opens and navigates to https://example.com/xpath-axes-sample | — | PASS |
| 2 | Waits until child element located by XPath using child axis is present | Page loaded with a div#parent containing span.child and span.sibling elements | Checks child.text equals 'Child Element' | PASS |
| 3 | Finds parent element of child using parent axis XPath | Parent div element with id='parent' found | Checks parent.get_attribute('id') equals 'parent' | PASS |
| 4 | Finds sibling element of child using following-sibling axis XPath | Sibling span element with class='sibling' found next to child | Checks sibling.text equals 'Sibling Element' | PASS |
| 5 | Browser closes and test ends | Browser window closed | — | PASS |
parent:: axis selects the direct parent of the current node.<li> elements of a <ul> element?child:: axis selects direct children of the current node.li elements//ul/child::li selects all li children of ul.<div> <h2>Title</h2> <p>Paragraph 1</p> <p>Paragraph 2</p> </div>
//h2/following-sibling::p[1] select?following-sibling::p[1] selects the first p sibling after the h2 node.h2, the first p is "Paragraph 1", so it selects that element.p instead of the firstdiv element://div/preceding-sibling::div[1]preceding-sibling::div[1] selects the immediate previous sibling div element.div has no preceding sibling div.span elements that are children of a div whose immediate parent is a section. Which XPath expression correctly uses axes to achieve this?span elements are children of div, and that div is a child of section.//section/child::div/child::span selects span children of div children of section.//div/parent::section/child::span selects span children of section, which is incorrect. //section/parent::div/child::span and D misuse axes or order.Pass the test and this pattern moves unknown