What is the use of Annotation "@FindBy"?

mra419 picture mra419 · Jan 27, 2012 · Viewed 56.1k times · Source

Can anyone explain me about Annotation @FindBy in WebDriver?

Where and why it is used?

Answer

el roso picture el roso · Feb 4, 2012

It's to assist with the construction of locators when using the Page Factory to support your Page Objects

PageFactory Wiki Page

However I'm discovering that I find it more useful to store your locators as By objects rather than WebElements as they are more flexible and you tend to avoid running into the StaleElementException.

By myLocator = By.id("idOfYourElement")

instead of

@FindBy(id = "idOfYourElement")
WebElement myLocator;

This way you can also use your locators when asserting the absence of an element or use it in the ExpectedConditions helpers.