Test Overview
This test opens a webpage, finds a button using XPath with an attribute, clicks it, and verifies the expected text appears.
This test opens a webpage, finds a button using XPath with an attribute, clicks it, and verifies the expected text appears.
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 TestXPathWithAttributes(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_click_button_by_xpath_attribute(self): driver = self.driver # Wait until button with attribute id='submit-btn' is present button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//button[@id='submit-btn']")) ) button.click() # Verify that a div with id='result' contains text 'Success' result_div = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.XPATH, "//div[@id='result']")) ) self.assertIn('Success', result_div.text) 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 is open and ready | - | PASS |
| 2 | Browser navigates to 'https://example.com/testpage' | Page loads with a button having id='submit-btn' and a hidden result div | - | PASS |
| 3 | Wait until button with XPath //button[@id='submit-btn'] is present | Button is found on the page | Button element is located successfully | PASS |
| 4 | Click the button found by XPath | Button is clicked, triggering page update | - | PASS |
| 5 | Wait until div with XPath //div[@id='result'] is visible | Result div appears with text content | Result div is visible | PASS |
| 6 | Check that result div text contains 'Success' | Result div text is 'Success' | Assert 'Success' in result_div.text | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |