0
0
Selenium Javatesting~5 mins

@FindBy annotations in Selenium Java

Choose your learning style9 modes available
Introduction

@FindBy annotations help you find web elements on a page easily without writing long code. They make your test code cleaner and easier to read.

When you want to locate a button on a webpage to click it in your test.
When you need to find a text box to enter some text during a test.
When you want to check if a specific element is visible on the page.
When you want to organize your element locators neatly in a Page Object Model class.
Syntax
Selenium Java
@FindBy(how = How.ID, using = "elementId")
private WebElement elementName;

You can use different locator types like id, name, xpath, css, etc.

The annotation is placed above the WebElement variable declaration.

Examples
Finds the element with id 'submitBtn'.
Selenium Java
@FindBy(id = "submitBtn")
private WebElement submitButton;
Finds the element with name 'username'.
Selenium Java
@FindBy(name = "username")
private WebElement usernameField;
Finds the element using an XPath expression.
Selenium Java
@FindBy(xpath = "//div[@class='alert']")
private WebElement alertMessage;
Finds the second item in a navigation menu using CSS selector.
Selenium Java
@FindBy(css = ".nav-menu > li:nth-child(2)")
private WebElement secondMenuItem;
Sample Program

This example shows a simple login page class using @FindBy annotations to locate username, password, and login button elements. The test class opens the browser, navigates to the login page, and performs a login.

Selenium Java
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 usernameInput;

    @FindBy(id = "password")
    private WebElement passwordInput;

    @FindBy(id = "loginBtn")
    private WebElement loginButton;

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

    public void login(String user, String pass) {
        usernameInput.sendKeys(user);
        passwordInput.sendKeys(pass);
        loginButton.click();
    }
}

// Test class example
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTest {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com/login");

        LoginPage loginPage = new LoginPage(driver);
        loginPage.login("testuser", "testpass");

        System.out.println("Login attempted");
        driver.quit();
    }
}
OutputSuccess
Important Notes

Always initialize elements with PageFactory.initElements() in the constructor.

Use meaningful variable names for WebElements to keep code readable.

Prefer specific locators like id or name over complex XPath for better speed and reliability.

Summary

@FindBy annotations simplify locating web elements in Selenium tests.

They help keep test code clean and organized, especially with Page Object Model.

Remember to initialize elements with PageFactory before using them.