Can anyone help me by answering the difference between @By
and @Findby
while using Selenium through Page Object Model.

- 183,867
- 41
- 278
- 352

- 11
- 1
- 2
4 Answers
After using both the ways, I can say that the major advantage of using annotation increases code readability and ensures less maintenance efforts!

- 11
- 1
In PageFactory - its only @FindBy or @FindBys finder annotation which is actually an interface. There is no '@By' annotation its just By and- 'By' is an abstract class in selenium.
Both of them used to form object repositories i.e. get the elements locators. And the usage of both depends on the way your automation framework has been designed. i.e. if you have used PageFactory pattern you will use @FindBy/s and for non pagefactory frameworks you can use By locator method
For ex- Use of @FindBy-
@FindBy(xpath = "//button[contains(.,'Ok')]")
WebElement okBtn;
Use of By-
By okBtn = By.xpath("//button[contains(.,'Ok')]");
or use it directly-
driver.findElement(By.xpath("//button[contains(.,'Ok')]")).click();

- 1,436
- 13
- 19
@FindBy using page factory design pattern, By is using for locating elements(Both are same but using situation is different)
@FindBy(linkText ="REGISTER")
private WebElement register_menu_element;

- 940
- 1
- 7
- 13
PageFactory
in Selenium collects all of the WebElement
s annotated with @FindBy
or @FindBys
or @FindAll
annotation and based on the value of the annotation builds By
object, which contains information about selector and its value.
PageFactory
also supports lazy instantiation. PageFactory
tries to find the element only when you try to interact with it.

- 3,528
- 1
- 13
- 27