Bird
0
0

In Selenium Java, which of the following is the proper way to initialize a WebElement using the PageFactory in a Page Object class?

easy📝 Syntax Q3 of 15
Selenium Java - Page Object Model
In Selenium Java, which of the following is the proper way to initialize a WebElement using the PageFactory in a Page Object class?
A@FindBy(id = "username") private WebElement usernameField;
BWebElement usernameField = driver.findElement(By.id("username"));
Cprivate WebElement usernameField = new WebElement();
DWebElement usernameField = driver.getElementById("username");
Step-by-Step Solution
Solution:
  1. Step 1: Understand PageFactory usage

    PageFactory uses annotations like @FindBy to locate elements and initialize them automatically.
  2. Step 2: Identify correct syntax

    @FindBy(id = "username") private WebElement usernameField; correctly uses @FindBy with the locator strategy and declares the WebElement as private.
  3. Final Answer:

    @FindBy(id = "username") private WebElement usernameField; -> Option A
  4. Quick Check:

    Annotation-based element declaration is standard in POM [OK]
Quick Trick: Use @FindBy annotation with private WebElement [OK]
Common Mistakes:
  • Using driver.findElement inside the Page Object class fields
  • Instantiating WebElement directly with new keyword
  • Using incorrect method names like getElementById

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes