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;
String title = (String) js.executeScript("return document.title;");
System.out.println(title);
APrints the URL of the current webpage
BThrows a ClassCastException at runtime
CPrints 'null' because executeScript returns void
DPrints the title of the current webpage
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the executeScript call

    The script returns document.title, which is the page title as a string.
  2. Step 2: Understand casting and output

    The returned Object is cast to String and printed, so the page title is printed.
  3. Final Answer:

    Prints the title of the current webpage -> Option D
  4. Quick Check:

    executeScript return value = page title string [OK]
Quick Trick: executeScript can return values like page title [OK]
Common Mistakes:
MISTAKES
  • Assuming executeScript returns void
  • Confusing document.title with URL
  • Expecting ClassCastException without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes