Bird
0
0

What is the issue with the following Selenium Java code snippet?

medium📝 Debug Q6 of 15
Selenium Java - JavaScriptExecutor
What is the issue with the following Selenium Java code snippet?
JavascriptExecutor js = driver;
js.executeScript("alert('Test');");
Aalert function is not supported in JavaScript execution
BexecuteScript method does not accept string arguments
Cdriver is not cast to JavascriptExecutor before assignment
DMissing import for JavascriptExecutor interface
Step-by-Step Solution
Solution:
  1. Step 1: Analyze assignment

    JavascriptExecutor is an interface; driver is a WebDriver instance.
  2. Step 2: Casting requirement

    To assign driver to JavascriptExecutor, explicit cast is needed: (JavascriptExecutor) driver.
  3. Step 3: Other options invalid

    executeScript accepts string, alert is valid JS, imports are unrelated to this error.
  4. Final Answer:

    driver is not cast to JavascriptExecutor before assignment -> Option C
  5. Quick Check:

    Always cast driver to JavascriptExecutor [OK]
Quick Trick: Cast driver to JavascriptExecutor before using executeScript [OK]
Common Mistakes:
MISTAKES
  • Assigning driver directly without casting
  • Assuming executeScript accepts non-string arguments only
  • Ignoring JavaScript alert support

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes