Test Overview
This test opens a webpage, finds a button using XPath with the text() function, clicks it, and verifies the expected message appears.
This test opens a webpage, finds a button using XPath with the text() function, clicks it, and verifies the expected message 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 TestXPathText(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/buttonpage') def test_click_button_by_text(self): driver = self.driver # Wait until the button with exact text 'Click Me' is present button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//button[text()='Click Me']")) ) button.click() # Wait for the message element to appear message = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'message')) ) self.assertEqual(message.text, 'Button clicked!') 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 navigated to 'https://example.com/buttonpage' | — | PASS |
| 2 | Wait until button with text 'Click Me' is present using XPath //button[text()='Click Me'] | Button with text 'Click Me' is visible on the page | Element located by XPath with exact text 'Click Me' | PASS |
| 3 | Click the button found by XPath | Button is clicked, page reacts to click | — | PASS |
| 4 | Wait until element with ID 'message' appears | Message element is visible on the page | Element with ID 'message' is present | PASS |
| 5 | Check that message text equals 'Button clicked!' | Message text is 'Button clicked!' | Assert message.text == 'Button clicked!' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | — | PASS |
//button[text()='Submit'] select in a web page?//button selects all button elements in the page.text() condition[text()='Submit'] filters buttons whose visible text exactly matches 'Submit'.<a>) with the visible text exactly 'Home'?text()='Home' inside square brackets.contains() which matches partial text, not exact. //a[@text='Home'] incorrectly treats text as an attribute. //a[text() = Home] misses quotes around 'Home'.<div> <span>Login</span> <span>Logout</span> <span>Login</span> </div>
elements = driver.find_elements(By.XPATH, "//span[text()='Login']") print(len(elements))
//span[text()='Login'] selects all <span> elements with exact text 'Login'. There are two such spans in the HTML.find_elements method returns a list of matching elements. Its length is 2.//button[text()='Cancel']. But your test fails to find the button. What is the most likely reason?//button[text()='Cancel'] is syntactically correct, so syntax error is unlikely.text() function matches exact visible text including spaces and case. If the button text has extra spaces or different case (e.g., ' cancel ' or 'CANCEL'), it won't match.<div> element that contains exactly the text 'Error: Invalid input' but the text may have leading or trailing spaces. Which XPath expression correctly handles this using text() and normalize-space()?text()='Error: Invalid input' won't match if spaces differ.normalize-space(text()) removes leading and trailing spaces before comparison, ensuring correct match.normalize-space() without text(), which may fail if element has multiple text nodes.Pass the test and this pattern moves unknown