Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Use the @FindBy Annotation in Selenium with Java

A practical guide to Selenium’s @FindBy annotation in Java: field declarations, locator strategies, PageFactory initialization, lazy lookup, caching, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium’s @FindBy annotation to declare how a Page Object locates a web element, then call PageFactory.initElements(driver, this) to initialize the page’s fields. PageFactory supplies a proxy that looks up the element when you use it; by default, it repeats the lookup on each method call.

Declare and initialize a Page Object

For a single element, declare a WebElement field and give @FindBy one locator strategy. Initialize the object with PageFactory, usually in its constructor:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

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

    @FindBy(css = "button[type='submit']")
    private WebElement submitButton;

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

    public void signIn(String user) {
        username.sendKeys(user);
        submitButton.click();
    }
}

Use the page object after constructing it with the active driver:

LoginPage loginPage = new LoginPage(driver);
loginPage.signIn("sample-user");

The annotation marks a field with an alternative locating mechanism and is intended for use with PageFactory. Declaring the annotation alone does not initialize the field; without PageFactory decoration, ordinary Java code may encounter a null field. Selenium FindBy API · Selenium PageFactory API

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a locator strategy

The concise form uses a single annotation attribute. Selenium also supports the explicit how and using form; these express the same locator intent.

Strategy Example Use when
ID @FindBy(id = "username") The element has a suitable ID in the page markup.
Name @FindBy(name = "email") The element has a suitable name attribute.
CSS selector @FindBy(css = "input[type='email']") A CSS selector expresses the target clearly.
XPath @FindBy(xpath = "//button[@type='submit']") The target requires an XPath expression.
Link text @FindBy(linkText = "Continue") The target is a link with matching visible text.
Partial link text @FindBy(partialLinkText = "Contin") A partial visible-text match is appropriate.
Class name @FindBy(className = "primary-button") The target has a usable class name.
Tag name @FindBy(tagName = "button") The tag identifies the intended element sufficiently.

The corresponding explicit form looks like this:

import org.openqa.selenium.support.How;

@FindBy(how = How.ID, using = "username")
private WebElement username;

Which locator is best depends on the application’s actual DOM. Prefer a locator that communicates the intended target and remains meaningful as the markup changes; no strategy is universally most resilient.

Use lists and unannotated fields carefully

For repeated matches, use List<WebElement> and an explicit locator:

import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

@FindBy(css = "ul.results > li")
private List<WebElement> results;

An unannotated field can be located using its Java field name as an ID or name. That default may be convenient for a single element, but an explicit locator makes the intended DOM match clearer. Prefer an explicit locator for list fields: Selenium’s project wiki has historical guidance that lists were decorated only when annotated and that the default ID/name behavior was poorly suited to lists; the wiki example was edited in 2015, so treat it as older guidance. Selenium project wiki: PageFactory

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand lazy lookup and caching

PageFactory.initElements decorates element fields and lists with proxies. The proxy performs the lookup when a method is called on the field, rather than necessarily locating every element when the page object is constructed. By default, each method call triggers a lookup. This can be useful when the page changes between interactions, but it also means that repeated calls can perform repeated lookups. PageFactory API

@CacheLookup requests that the located element be reused from cache on later calls. Use it only when the element is stable enough that retaining the cached reference is appropriate; a cached element can be unsuitable if the page replaces or refreshes that element. Selenium CacheLookup API

Common errors and fixes

  • Field is null: the annotation does not initialize itself. Ensure the page object is created or decorated with PageFactory.initElements(driver, pageObject) before using the field.
  • Element cannot be found: confirm that the locator matches the current DOM and that the relevant page state has loaded before interacting. Selenium supports the listed locator strategies, but the correct one depends on the markup.
  • Unexpected default match: an unannotated field uses its field name as an ID or name locator. Add an explicit @FindBy when that fallback is not the intended target.
  • Invalid combination of annotations: keep to one recognized locator annotation per field. The Selenium annotations API documents an IllegalArgumentException when more than one of @FindBy, @FindBys, and @FindAll is present on the same field. Selenium Annotations API
  • Cached reference no longer works: if the application replaces an element during navigation or an update, reconsider @CacheLookup so the proxy can perform a fresh lookup.

Or skip the browser setup

If the goal is to capture a page rather than interact with it through Selenium, ScreenshotNeo offers a one-request screenshot API. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots. Learn about ScreenshotNeo or sign up free for 1,000 screenshots a month with no card.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Can I put @FindBy on a class instead of a field?

The annotation API permits type-level use, but type-level annotations are not processed by default. For the usual PageFactory workflow, put it on the element field.

Can @FindBy locate more than one element?

Yes. Declare a `List` field and give it an explicit locator, such as `@FindBy(css = “ul.results > li”)`.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.