Bird
0
0

Consider this Base Page method:

medium📝 Debug Q7 of 15
Selenium Java - Page Object Model
Consider this Base Page method:
public boolean checkElementVisible(By locator) {
  try {
    WebElement element = driver.findElement(locator);
    return element.isDisplayed();
  } catch (Exception e) {
    return false;
  }
}

What is a potential problem with this implementation?
Adriver.findElement returns a list, so this code will not compile
BThe method should throw exceptions instead of returning false
CUsing isDisplayed() is incorrect; should use isEnabled()
DCatching generic Exception may hide unexpected errors; better to catch NoSuchElementException
Step-by-Step Solution
Solution:
  1. Step 1: Understand Exception Handling

    Catching generic Exception can mask other issues beyond element absence.
  2. Step 2: Identify Best Practice

    It's better to catch specific exceptions like NoSuchElementException to handle element absence gracefully.
  3. Step 3: Reasoning

    This improves debugging and avoids hiding unrelated errors.
  4. Final Answer:

    Catching generic Exception may hide unexpected errors; better to catch NoSuchElementException -> Option D
  5. Quick Check:

    Catch specific exceptions, not generic [OK]
Quick Trick: Catch specific exceptions like NoSuchElementException [OK]
Common Mistakes:
MISTAKES
  • Catching all exceptions indiscriminately
  • Confusing isDisplayed() with isEnabled()
  • Misunderstanding return types of findElement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes