What if your tests could find buttons by their labels just like you do, without breaking when the page changes?
Why XPath with text() in Selenium Java? - Purpose & Use Cases
Imagine you need to find a button on a webpage by reading its label manually every time you test.
You open the page, look for the exact text on the button, and click it yourself.
This manual way is slow and tiring.
You might miss the button if the text changes slightly or if there are many similar buttons.
It's easy to make mistakes and waste time repeating this for every test.
Using XPath with text() lets you tell the computer to find elements by their visible text automatically.
This means your test can click the right button even if its position changes, as long as the text stays the same.
driver.findElement(By.id("button1")).click(); // hardcoded id, fails if id changes
driver.findElement(By.xpath("//button[text()='Submit']")).click(); // finds button by visible textYou can write tests that find elements by what users actually see, making tests more reliable and easier to maintain.
On a shopping site, you want to click the "Add to Cart" button no matter where it appears on the page.
Using XPath with text(), your test finds and clicks it by its label, even if the page layout changes.
Manual searching by text is slow and error-prone.
XPath with text() automates finding elements by visible text.
This makes tests more stable and easier to update.