Selenium · Browser automation foundations

How does PageFactory work and what are its pitfalls?

Mediumpagefactorypage objectsannotationsUpdated Oct 2026
Quiz this track

Short answer

PageFactory initialises fields annotated with @FindBy and wraps them in lazy proxies that locate the element when used. It reduces boilerplate, but proxies can hide stale elements and caching behaviour can cause confusing failures.

PageFactory is a convenience layer for page objects. Instead of writing findElement in every method, you declare a field and annotate it. initElements creates the proxies. The element is not found at construction time; it is located when the field is first used.

java
public class LoginPage {
    @FindBy(id = "email")
    private WebElement email;

    @FindBy(css = "[data-testid='sign-in']")
    private WebElement signIn;

    public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

    public void signIn(String user) {
        email.sendKeys(user);
        signIn.click();
    }
}
Fields are proxies; the underlying lookup happens on use.
  • initElements must be called before the fields are used
  • Proxies re-locate on each use, which can reduce stale element failures
  • @CacheLookup caches the element after the first lookup and can go stale after re-renders
  • An AjaxElementLocatorFactory adds a lazy wait to the proxy lookup
  • PageFactory does not make a bad locator stable; it only changes when the lookup happens
  • Modern teams often prefer explicit By fields or a component library instead of annotations

The most common bug is combining PageFactory with a driver that changes between tests while reusing a cached page object. Another is assuming the field is resolved at construction, then checking for a missing element in the constructor. Be explicit about when lookup happens.

PageFactory is optional. If the team finds annotations harder to navigate than explicit locators, not using it is a valid design choice.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1When is the element actually located with @FindBy?
  2. 2Why can @CacheLookup cause failures?
  3. 3How does AjaxElementLocatorFactory change behaviour?
  4. 4Would you choose PageFactory for a new suite?

Say it in the interview

PageFactory turns @FindBy fields into lazy proxies, so the element is located when used rather than when the page object is constructed. That reduces boilerplate and can reduce staleness. The pitfalls are caching with @CacheLookup and confusion about lookup timing. I use it only if the team is comfortable with the proxy model; explicit By fields are often easier to debug.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.