Bird
0
0

Which of the following is the correct way to declare a locator in a Selenium page class using Java?

easy📝 Syntax Q12 of 15
Selenium Java - Page Object Model
Which of the following is the correct way to declare a locator in a Selenium page class using Java?
Apublic void submitButton = driver.findElement(By.id("submit"));
Bpublic String submitButton = "submit";
Cprivate WebElement submitButton = driver.findElement(By.id("submit"));
Dprivate By submitButton = By.id("submit");
Step-by-Step Solution
Solution:
  1. Step 1: Recall best practice for locators

    Locators should be stored as By objects, not WebElement, to delay element lookup.
  2. Step 2: Check each option

    private By submitButton = By.id("submit"); correctly declares a private By locator with By.id("submit").
  3. Final Answer:

    private By submitButton = By.id("submit"); -> Option D
  4. Quick Check:

    Locators = By objects [OK]
Quick Trick: Use By for locators, not WebElement directly [OK]
Common Mistakes:
  • Declaring locators as WebElement fields
  • Using public visibility for locators
  • Assigning void type to variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes