Bird
0
0

Identify the error in this page class method:

medium📝 Debug Q14 of 15
Selenium Java - Page Object Model
Identify the error in this page class method:
public void clickLogin() {
  WebElement loginBtn = driver.findElement(By.id("login"));
  loginBtn.click();
}
ANo error, method is correct
BShould store locator as a By field, not find element inside method
CMissing exception handling for element not found
DMethod should return WebElement instead of void
Step-by-Step Solution
Solution:
  1. Step 1: Review locator usage best practice

    Locators should be stored as By fields in the page class, not found inside methods.
  2. Step 2: Identify the problem

    Finding element inside method reduces reuse and violates page class design principles.
  3. Final Answer:

    Should store locator as a By field, not find element inside method -> Option B
  4. Quick Check:

    Locators as By fields = best practice [OK]
Quick Trick: Keep locators as fields, find elements in methods [OK]
Common Mistakes:
  • Finding elements repeatedly inside methods
  • Ignoring locator reuse
  • Assuming exception handling is mandatory here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes