Bird
0
0

You want to scroll to a list of elements one by one and click each after scrolling into view. Which approach correctly combines scrolling and clicking in Selenium Java?

hard📝 Application Q15 of 15
Selenium Java - JavaScriptExecutor
You want to scroll to a list of elements one by one and click each after scrolling into view. Which approach correctly combines scrolling and clicking in Selenium Java?
Afor(WebElement el : elements) { driver.executeScript("scrollIntoView(el)"); el.click(); }
Bfor(WebElement el : elements) { el.click(); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); }
Cfor(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); }
Dfor(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("scrollIntoView(true);"); el.click(); }
Step-by-Step Solution
Solution:
  1. Step 1: Scroll element into view before clicking

    Scrolling must happen before clicking to ensure element is visible and interactable.
  2. Step 2: Validate correct JavascriptExecutor usage

    for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); } correctly casts driver, passes element as argument, and clicks after scrolling.
  3. Final Answer:

    for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); } -> Option C
  4. Quick Check:

    Scroll then click each element = for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); } [OK]
Quick Trick: Scroll each element before clicking it in the loop [OK]
Common Mistakes:
  • Clicking before scrolling
  • Incorrect JavascriptExecutor syntax
  • Not passing element argument in script

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes