Bird
0
0

What will be the output of the following Selenium Java code snippet?

medium📝 Predict Output Q4 of 15
Selenium Java - JavaScriptExecutor
What will be the output of the following Selenium Java code snippet?
JavascriptExecutor js = (JavascriptExecutor) driver;
Long height = (Long) js.executeScript("return window.innerHeight;");
System.out.println(height);
AAn error because window.innerHeight is not accessible via JavaScriptExecutor
BThe total height of the webpage including content outside the viewport
CThe height of the HTML document's body element
DThe height of the browser's viewport in pixels
Step-by-Step Solution
Solution:
  1. Step 1: Understand the JavaScript executed

    The script "return window.innerHeight;" returns the height of the viewport (visible area) of the browser window in pixels.
  2. Step 2: Casting the returned value

    The returned value is a JavaScript number, which maps to a Long in Java when cast from Object.
  3. Final Answer:

    The height of the browser's viewport in pixels -> Option D
  4. Quick Check:

    window.innerHeight returns viewport height [OK]
Quick Trick: window.innerHeight returns viewport height [OK]
Common Mistakes:
MISTAKES
  • Confusing window.innerHeight with document.body.scrollHeight
  • Expecting total page height instead of viewport height
  • Assuming JavaScriptExecutor cannot access window properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes