Make Selenium Easy

Is It Possible To Use Page Factory Without FindBy In Selenium WebDriver?

Hello Folks,

We are learning about Page Object Model in selenium in previous posts. This is another part of that.

Selenium webdriver has inbuilt page object model which is called Page Factory where we write web elements(Page Objects) with it locators using annotation @FindBy. Does FindBy annotation mandatory for Page Factory?

Answer is NO.

We do not require to use FindBy annotation for a page object if it has id or name. Page Factory sets a lazy proxy for each of the WebElement and List<WebElement> fields that have been declared, assuming that the field name is also the HTML element’s “id” or “name”. This means that for the class:

public class Page
{
private WebElement submit;
}
there will be an element that can be located using the xpath expression “//*[@id=’submit’]” or “//*[@name=’submit’]”.

So below lines of code are same:

@FindBy(id=”SomeId”)
WebElement elementName;

AND

WebElement SomeId;

So to use other locators like xpath, css etc, we required FindBy annotation.
Example:

RegistrationPage.java:

Registration Script:

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Table of Contents

Author: Amod Mahajan

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

1 thought on “Is It Possible To Use Page Factory Without FindBy In Selenium WebDriver?

  1. can u clarify below line of code,
    how driver will identify the element in above code we have not sent any id or name of locator

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.