Discover how a simple annotation can save you hours of frustrating test code rewriting!
Why @FindBy annotations in Selenium Java? - Purpose & Use Cases
Imagine you have a web page with many buttons, links, and input fields. You want to test if each element works correctly by clicking or typing on them manually every time you run your test.
Manually finding each element on the page every time is slow and tiring. You might click the wrong button or miss an element. Writing code that searches for elements again and again makes your tests messy and hard to fix when the page changes.
@FindBy annotations let you tell your test exactly where to find each element once. Then, your test can use these ready references easily. This keeps your code clean, faster, and easier to update if the page changes.
WebElement button = driver.findElement(By.id("submit"));
button.click();@FindBy(id = "submit")
private WebElement button;
// Use this inside a method after initializing PageFactory
button.click();It enables writing clear, reusable, and maintainable tests that quickly interact with web elements without repeating locator code.
When testing an online shopping site, you can use @FindBy to locate the 'Add to Cart' button once and click it many times in different tests without rewriting the locator.
Manual element searching is slow and error-prone.
@FindBy annotations store element locators cleanly in one place.
This makes tests easier to write, read, and maintain.