What if your test could find buttons by their words, not by guessing their hidden codes?
Why XPath with text() in Selenium Python? - Purpose & Use Cases
Imagine you need to find a button on a webpage that says "Submit". You try to click it by guessing its position or ID, but the page changes often, and your guesses fail.
Manually searching for elements by position or guessing IDs is slow and often breaks when the page layout changes. It's like trying to find a book in a messy library without a catalog.
Using XPath with text() lets you find elements by their visible text, like searching the library catalog by book title. This makes your tests more reliable and easier to maintain.
driver.find_element(By.ID, "btn1").click()driver.find_element(By.XPATH, "//button[text()='Submit']").click()You can precisely target elements by their displayed text, making tests stable even if page structure changes.
When a website changes button IDs but keeps the button text "Login", your test still finds and clicks the login button without breaking.
Manual element finding by ID or position is fragile.
XPath with text() targets elements by visible text.
This approach makes tests more reliable and easier to update.