Bird
0
0

You wrote this Selenium Java code to scroll to an element:

medium📝 Debug Q14 of 15
Selenium Java - JavaScriptExecutor
You wrote this Selenium Java code to scroll to an element:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();");

But it throws an error. What is the likely cause?
AIncorrect casting of driver to JavascriptExecutor
BscrollIntoView() is not a valid JavaScript method
CMissing the element argument in executeScript call
DexecuteScript cannot scroll the page
Step-by-Step Solution
Solution:
  1. Step 1: Check executeScript arguments

    The script uses arguments[0] but no element argument is passed, causing an error.
  2. Step 2: Confirm correct usage

    To scroll to an element, you must pass the WebElement as a second argument to executeScript.
  3. Final Answer:

    Missing the element argument in executeScript call -> Option C
  4. Quick Check:

    arguments[0] needs element argument [OK]
Quick Trick: Pass element as argument when using arguments[0] in JS [OK]
Common Mistakes:
  • Forgetting to pass element argument
  • Thinking scrollIntoView() is invalid
  • Miscasting driver to JavascriptExecutor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes