2

I have recently come across @FindBy as a method of locating elements within webdriver. What are the benefits or disadvantages of this approach compared with the to more common approach of using findElement?

Below are examples of the two methods;

WebElement searchButton = driver.findElement(By.id("btnSearch"))

@FindBy(id = "btnSearch")
private WebElement searchButton;
1

Generally speaking they are the same, @FindBy might be more suitable for PageObject Pattern.

Have a look at here

0

By using the @FindBy you're pulling the initialization of the code out of dirty, clunky, visually ugly and potentially confusing code into nice neat annotations.

Also, your removing the WebDriver nearly completely from the object, again keeping it clean.

They do the same functionality practically though.

Makes maintaining the code far easier long term.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.