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 |