Selenium + Java · Lesson 24 of 44
Module 24: JavaScript Executor
Your task
Implement scrollIntoViewAndClick:
Write it in the editor, then press Check answer (Ctrl + Enter).
import org.openqa.selenium.JavascriptExecutor;
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll element into view:
WebElement footer = driver.findElement(By.id("footer"));
js.executeScript("arguments[0].scrollIntoView(true);", footer);
// JS Click (bypasses click interception):
WebElement submitBtn = driver.findElement(By.id("submit"));
js.executeScript("arguments[0].click();", submitBtn);
// Retrieve page document readyState:
String state = (String) js.executeScript("return document.readyState;");When WebDriver fails to click due to overlay interception or needs to scroll deep down a long page, JavascriptExecutor provides a direct bridge into the browser's JavaScript engine. In this lesson, you will learn when to use and when to avoid executeScript. Let's master JavascriptExecutor!
Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
No! JS clicks bypass user-facing checks (e.g. checking if element is hidden or disabled), potentially giving false positive test passes. Use it only when necessary.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___