Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q5 of 15
Selenium Java - JavaScriptExecutor
Given the code below, what will be the output?
JavascriptExecutor js = (JavascriptExecutor) driver;
Boolean isVisible = (Boolean) js.executeScript("return document.getElementById('btn').offsetParent !== null;");
System.out.println(isVisible);
AAlways prints false
BThrows NullPointerException if element not found
Ctrue if element with id 'btn' is visible, false otherwise
DPrints the element's HTML content
Step-by-Step Solution
Solution:
  1. Step 1: Understand offsetParent property

    offsetParent is null if element is hidden or not in DOM; otherwise not null.
  2. Step 2: JavaScript returns boolean visibility

    The script returns true if element is visible, false if hidden or missing.
  3. Final Answer:

    true if element with id 'btn' is visible, false otherwise -> Option C
  4. Quick Check:

    offsetParent check returns visibility boolean = A [OK]
Quick Trick: Use offsetParent to check element visibility via JS [OK]
Common Mistakes:
  • Expecting exception if element missing
  • Assuming offsetParent always true
  • Confusing visibility with content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes