Selenium + Java · Lesson 17 of 44
Module 17: Locators
Your task
Implement createDynamicButtonXPath:
Write it in the editor, then press Check answer (Ctrl + Enter).
By.id("login-btn") - Fastest & uniqueBy.cssSelector("button[data-testid='submit']") - Fast & readableBy.xpath("//button[contains(text(), 'Submit')]") - Powerful text searchBy.xpath("//td[text()='ORD-101']/following-sibling::td//button") - XPath axes// XPath Axes:
// Find delete button for a specific table user:
By deleteBtn = By.xpath("//tr[td[contains(text(), 'Dhanu')]]//button[@aria-label='Delete']");In Selenium, flaky locators are the number one cause of test failures. In this lesson, we master By.id, By.cssSelector, and relative XPath with contains, starts-with, and XPath axes like following-sibling and ancestor. Let's build resilient locators!
Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
Any minor DOM change or newly added header container immediately breaks absolute paths. Always use relative XPath with attributes or text.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___