Using the @FindBy annotation

An element in the PageObject is marked with the @FindBy annotation. It is used to direct the WebDriver to locate that element on a page. It takes the locating mechanism (that is, by Id, Name, or Class Name) and the value of the element for that locating mechanism as input.

There are two ways of using the @FindBy annotation:

Usage 1 is shown as follows:

@FindBy(id="user_login")WebElement userId;

Usage 2 is shown as follows:

@FindBy(how=How.ID, using="user_login")WebElement userId; 

The preceding two usages direct the WebDriver to locate the element using the locating mechanism ID with the user_login value and assign that element to the userId WebElement. In usage 2, we have used the How enumeration. This enumeration ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.