Bird
0
0

Given the following code snippet, what will be the output if the element is not found on the page?

medium📝 Predict Output Q4 of 15
Selenium Java - Page Object Model
Given the following code snippet, what will be the output if the element is not found on the page?
public class LoginPage {
  @FindBy(id = "submitBtn")
  private WebElement submitButton;

  public LoginPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }

  public boolean isSubmitButtonDisplayed() {
    return submitButton.isDisplayed();
  }
}
AReturns false without exception
BThrows NoSuchElementException at runtime
CReturns true always
DCompilation error due to missing element
Step-by-Step Solution
Solution:
  1. Step 1: Understand PageFactory lazy loading behavior

    PageFactory creates proxies for elements but actual lookup happens on method call.
  2. Step 2: Behavior when element is missing

    Calling isDisplayed() on a missing element throws NoSuchElementException at runtime.
  3. Final Answer:

    Throws NoSuchElementException at runtime -> Option B
  4. Quick Check:

    Missing element access = NoSuchElementException [OK]
Quick Trick: Missing element causes runtime NoSuchElementException [OK]
Common Mistakes:
  • Assuming it returns false silently
  • Expecting compile-time errors
  • Thinking it returns true always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes