Bird
0
0

You want to locate multiple elements with the same class name using @FindBy. Which declaration is correct to store all matching elements in a list?

hard📝 Application Q15 of 15
Selenium Java - Page Object Model
You want to locate multiple elements with the same class name using @FindBy. Which declaration is correct to store all matching elements in a list?
public class ProductsPage {
  // Which line is correct?
}
A@FindBy(css = ".product-item") WebElement products;
B@FindBy(className = "product-item") WebElement products;
C@FindBy(xpath = "//div[@class='product-item']") WebElement products;
D@FindBy(className = "product-item") List<WebElement> products;
Step-by-Step Solution
Solution:
  1. Step 1: Understand locating multiple elements

    To store multiple elements, the field must be a List<WebElement>. Using @FindBy with className locator is valid.
  2. Step 2: Check each option's type and locator

    @FindBy(className = "product-item") List products; uses List<WebElement> with correct locator. Options A, B, and D use single WebElement, which can only hold one element.
  3. Final Answer:

    @FindBy(className = "product-item") List<WebElement> products; -> Option D
  4. Quick Check:

    Multiple elements need List = C [OK]
Quick Trick: Use List for multiple elements with @FindBy [OK]
Common Mistakes:
  • Declaring multiple elements as single WebElement
  • Using wrong locator type for multiple elements
  • Forgetting to import java.util.List

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes