Selenium Interview Question 4 – Difference Between @FindBy and findElement() Method in Selenium WebDriver

We can locate web element/s on a webpage using both FindBy and findElement() or findElements(). Primary purpose of both ways is same but the way it is done, is different.

Below are the differences between FindBy and findElement in Selenium WebDriver:

  1. FindBy is an annotation while findElement and findElements are methods. Remember FindBy is provided by Selenium not by TestNG. I saw some people have commented such differences.
  2. FindBy is used to support PageObject pattern through PageFactory while findElement is normal way of locating a web element.
  3. If we do not initialise page objects using PageFactory, @FindBy will throw NullPointerException which is not the case with findElement() or findElements() methods.
  4. FindBy will look for the element only when it is used while findElement will look for an element as soon as you call it.
  5. FindBy supports lazy initialization while findElement() and findElements does not.
  6. FindBy can be used to locate one or all using a locator but you need to use findElement to locate one element and findElements to locate all elements using a locator.
  7. You need to explicitly keep track of browser driver instance in FindBy annotated page elements i.e. using PageFactory class to initialize page elements while you call findElement() or findElements() directly on browser driver instance.
  8. FindBy helps to handle StaleElementException as it looks for element every time when it is called. If you use findElement() or findElements() method and store the web element, it will not be relocated again until you do it explicitly.
  9. FindBy supports PageObject pattern which helps you in designing neat tests. So Using FindBy is more preferable than findElement() Methods.

Something Interesting:

I saw some comments saying that FindBy also calls findElement() or findElements() internally. When I saw selenium source code, I found it calls findElement() and findElements() internally but it is not the same as normal findElement() and findElements() which we use.

Normal findElement() and findElements() are declared in WebDriver interface and implemented in RemoteWebDriver class.

While for FindBy, findElement() and findElements() are declared in Element Locator interface.

Class DefaultElementLocator implements ElementLocator. Documentation of this class says : The default element locator, which will lazily locate an element or an element list on a page. This class is

  • designed for use with the {@link} and understands the
  • annotations {@link} and {@link}.

We can use both FindBy and findElement() or findElements() together but it will not impact each other.

Refer more Selenium Interview Questions Here.

If you like my posts, please like, comment and share to reach maximum.


Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “Selenium Interview Question 4 – Difference Between @FindBy and findElement() Method in Selenium WebDriver

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.