Bird
0
0

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

medium📝 Predict Output Q13 of 15
Selenium Java - JavaScriptExecutor
What will be the output of the following Selenium Java code snippet?
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title;");
System.out.println(title);

Assuming the page title is "Welcome Page".
AWelcome Page
Bnull
Cdocument.title
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand JavaScript execution returning page title

    The JavaScript code "return document.title;" returns the current page title as a string.
  2. Step 2: Casting and printing the returned value

    The returned value is cast to String and printed, so output will be the page title Welcome Page.
  3. Final Answer:

    Welcome Page -> Option A
  4. Quick Check:

    JS returns title string [OK]
Quick Trick: JS returns page title string, print shows it [OK]
Common Mistakes:
  • Expecting JavaScript code as output
  • Forgetting to cast returned value
  • Assuming null if no explicit return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes