Bird
Raised Fist0
Selenium Javatesting~15 mins

Scrolling into view in Selenium Java - Build an Automation Script

Choose your learning style10 modes available
Scroll element into view and verify visibility
Preconditions (3)
Step 1: Open the browser and navigate to the test page URL
Step 2: Locate the target element that is initially not visible on the screen
Step 3: Scroll the page until the target element is visible
Step 4: Verify that the target element is displayed on the screen
✅ Expected Result: The target element is scrolled into view and is visible on the screen
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the target element is displayed after scrolling
Best Practices:
Use explicit waits to ensure the element is present before scrolling
Use JavaScriptExecutor to scroll the element into view
Use meaningful locators such as By.id or By.cssSelector
Avoid hardcoded waits like Thread.sleep
Close the browser after test execution
Automated Solution
Selenium Java
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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using hardcoded XPath locators that are brittle
Not waiting for element presence before scrolling
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different target element IDs to scroll into view and verify visibility.

Show Hint

Practice

(1/5)
1. What is the main purpose of using scrollIntoView() in Selenium Java?
easy
A. To scroll the webpage until the specified element is visible on the screen
B. To click an element without scrolling
C. To find an element by its locator
D. To wait for an element to be clickable

Solution

  1. Step 1: Understand the function of scrollIntoView()

    This method scrolls the page so that the element is visible in the viewport.
  2. Step 2: Compare with other options

    Options A, C, and D describe different actions unrelated to scrolling the page.
  3. Final Answer:

    To scroll the webpage until the specified element is visible on the screen -> Option A
  4. Quick Check:

    scrollIntoView() = scroll element into view [OK]
Hint: scrollIntoView() makes element visible by scrolling [OK]
Common Mistakes:
  • Confusing scrollIntoView() with click()
  • Thinking scrollIntoView() finds elements
  • Assuming scrollIntoView() waits for element
2. Which of the following is the correct syntax to scroll an element into view using JavascriptExecutor in Selenium Java?
easy
A. element.scrollIntoView();
B. driver.scrollIntoView(element);
C. driver.executeScript("scrollIntoView(element)");
D. ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

Solution

  1. Step 1: Recall correct JavascriptExecutor syntax

    In Selenium Java, JavascriptExecutor requires casting driver and passing script with arguments.
  2. 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.
  3. Final Answer:

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); -> Option D
  4. Quick Check:

    Correct JavascriptExecutor syntax = ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); [OK]
Hint: Cast driver to JavascriptExecutor and pass element as argument [OK]
Common Mistakes:
  • Calling scrollIntoView() directly on driver
  • Not casting driver to JavascriptExecutor
  • Passing element inside script string incorrectly
3. What will be the result of the following Selenium Java code snippet?
WebElement element = driver.findElement(By.id("footer"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", element);
System.out.println(element.isDisplayed());
medium
A. false
B. true
C. Throws NoSuchElementException
D. Throws JavascriptException

Solution

  1. Step 1: Understand scrollIntoView(false) effect

    scrollIntoView(false) scrolls so element's bottom aligns with viewport bottom, making it visible.
  2. Step 2: Check element visibility after scrolling

    Since element is scrolled into view, element.isDisplayed() returns true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    scrollIntoView(false) makes element visible = true [OK]
Hint: scrollIntoView(false) scrolls element bottom into view [OK]
Common Mistakes:
  • Assuming isDisplayed() returns false after scrolling
  • Confusing scrollIntoView(true) and false
  • Expecting exceptions without missing element
4. Identify the error in the following Selenium Java code snippet for scrolling an element into view:
WebElement element = driver.findElement(By.cssSelector(".my-element"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);");
medium
A. Missing element argument in executeScript method
B. driver is not cast to JavascriptExecutor before assignment
C. Incorrect CSS selector syntax
D. scrollIntoView method does not accept boolean argument

Solution

  1. Step 1: Check JavascriptExecutor assignment

    The assignment correctly casts driver to JavascriptExecutor.
  2. Step 2: Verify executeScript arguments

    executeScript requires the element as second argument to replace arguments[0], missing here.
  3. Final Answer:

    Missing element argument in executeScript method -> Option A
  4. Quick Check:

    executeScript needs element argument for arguments[0] [OK]
Hint: Pass element as second argument to executeScript [OK]
Common Mistakes:
  • Not casting driver to JavascriptExecutor
  • Forgetting to pass element argument
  • Misunderstanding scrollIntoView parameters
5. 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
A. for(WebElement el : elements) { driver.executeScript("scrollIntoView(el)"); el.click(); }
B. for(WebElement el : elements) { el.click(); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); }
C. for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el); el.click(); }
D. for(WebElement el : elements) { ((JavascriptExecutor) driver).executeScript("scrollIntoView(true);"); el.click(); }

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]
Hint: Scroll each element before clicking it in the loop [OK]
Common Mistakes:
  • Clicking before scrolling
  • Incorrect JavascriptExecutor syntax
  • Not passing element argument in script
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard