Scrolling into view in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import static org.junit.jupiter.api.Assertions.assertTrue; public class ScrollIntoViewTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/scrollable-page"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); By targetLocator = By.id("target-element"); // Wait until the element is present in DOM WebElement targetElement = wait.until(ExpectedConditions.presenceOfElementLocated(targetLocator)); // Scroll the element into view using JavaScriptExecutor ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", targetElement); // Wait until the element is visible wait.until(ExpectedConditions.visibilityOf(targetElement)); // Assert the element is displayed assertTrue(targetElement.isDisplayed(), "Target element should be visible after scrolling"); } finally { driver.quit(); } } }
This test script uses Selenium WebDriver with Java to automate scrolling an element into view.
First, it opens the browser and navigates to the test page URL.
It waits explicitly for the target element to be present in the page DOM to avoid timing issues.
Then it uses JavascriptExecutor to scroll the element into view, which is a reliable way to bring off-screen elements into the visible area.
After scrolling, it waits until the element is visible on the screen.
Finally, it asserts that the element is displayed, confirming the scroll worked.
The browser is closed in a finally block to ensure cleanup even if the test fails.
Now add data-driven testing with 3 different target element IDs to scroll into view and verify visibility.
Practice
scrollIntoView() in Selenium Java?Solution
Step 1: Understand the function of scrollIntoView()
This method scrolls the page so that the element is visible in the viewport.Step 2: Compare with other options
Options A, C, and D describe different actions unrelated to scrolling the page.Final Answer:
To scroll the webpage until the specified element is visible on the screen -> Option AQuick Check:
scrollIntoView() = scroll element into view [OK]
- Confusing scrollIntoView() with click()
- Thinking scrollIntoView() finds elements
- Assuming scrollIntoView() waits for element
Solution
Step 1: Recall correct JavascriptExecutor syntax
In Selenium Java, JavascriptExecutor requires casting driver and passing script with arguments.Step 2: Validate each option
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); uses correct casting and script with arguments[0]. Options A, B, and C are invalid or incomplete syntax.Final Answer:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); -> Option DQuick Check:
Correct JavascriptExecutor syntax = ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); [OK]
- Calling scrollIntoView() directly on driver
- Not casting driver to JavascriptExecutor
- Passing element inside script string incorrectly
WebElement element = driver.findElement(By.id("footer"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
System.out.println(element.isDisplayed());Solution
Step 1: Understand scrollIntoView(false) effect
scrollIntoView(false) scrolls so element's bottom aligns with viewport bottom, making it visible.Step 2: Check element visibility after scrolling
Since element is scrolled into view, element.isDisplayed() returns true.Final Answer:
true -> Option BQuick Check:
scrollIntoView(false) makes element visible = true [OK]
- Assuming isDisplayed() returns false after scrolling
- Confusing scrollIntoView(true) and false
- Expecting exceptions without missing element
WebElement element = driver.findElement(By.cssSelector(".my-element"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);");Solution
Step 1: Check JavascriptExecutor assignment
The assignment correctly casts driver to JavascriptExecutor.Step 2: Verify executeScript arguments
executeScript requires the element as second argument to replace arguments[0], missing here.Final Answer:
Missing element argument in executeScript method -> Option AQuick Check:
executeScript needs element argument for arguments[0] [OK]
- Not casting driver to JavascriptExecutor
- Forgetting to pass element argument
- Misunderstanding scrollIntoView parameters
Solution
Step 1: Scroll element into view before clicking
Scrolling must happen before clicking to ensure element is visible and interactable.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.Final Answer:
for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); } -> Option CQuick Check:
Scroll then click each element = for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); } [OK]
- Clicking before scrolling
- Incorrect JavascriptExecutor syntax
- Not passing element argument in script
Ready to prove it?
Pass the test and this pattern moves unknown
