We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Using Actions and WheelInput to scroll long pages and specific regions precisely
In this practice lab, you will learn how to control scrolling using the Selenium Actions API with WheelInput.
Instead of relying on:
element.click() (which sometimes scrolls implicitly), orwindow.scrollTo(...)),you will use scroll-specific actions:
scrollToElement(...)scrollByAmount(...)scrollFromOrigin(...) with different originsWhat it does
Scrolls the page so that the target element is brought into view. Typically, the bottom of the element aligns with the bottom of the viewport.
When to use it
What it does
Scrolls the page by a relative offset:
deltaX → horizontal scroll (right is positive, left is negative)deltaY → vertical scroll (down is positive, up is negative)When to use it
What it does
Scrolls from a specific origin point instead of the current position:
ScrollOrigin.fromElement(element) – based on an element's centerScrollOrigin.fromElement(element, offsetX, offsetY) – offset from centerScrollOrigin.fromViewport(offsetX, offsetY) – from viewport top-leftWhen to use it
When a portion of the screen or component needs to be scrolled in a controlled way.
Imagine a page called "Scroll Wheel Playground" with multiple sections:
This is the Hero Section (always visible at page load)
The page below simulates a long scrollable page with multiple sections. Click the buttons to see scroll actions in use.
Description of feature one
Description of feature two
Basic
$9
per month
Pro
$29
per month
Enterprise
$99
per month
Use the buttons above to simulate scroll actions, or scroll manually to explore the sections!
If you were doing this manually with a mouse:
Your Selenium script will automate a similar flow using scrollToElement(), scrollByAmount(), and scrollFromOrigin().
12345678910111213141516171819202122WebElement pricingSection = driver.findElement(By.id("pricing-section"));
WebElement footer = driver.findElement(By.id("page-footer"));
Actions actions = new Actions(driver);
// 1. Scroll so that the Pricing section comes into view
actions.scrollToElement(pricingSection).perform();
// 2. Scroll a bit further down from the current position
actions.scrollByAmount(0, 300).perform();
// 3. Scroll starting near the footer, with an offset above it
WheelInput.ScrollOrigin footerOrigin =
WheelInput.ScrollOrigin.fromElement(footer, 0, -50);
actions.scrollFromOrigin(footerOrigin, 0, 200).perform();
// 4. Scroll from a viewport-based origin near the top-left corner
WheelInput.ScrollOrigin viewportOrigin =
WheelInput.ScrollOrigin.fromViewport(10, 10);
actions.scrollFromOrigin(viewportOrigin, 0, -200).perform();
driver.quit();id="pricing-section", which is initially off-screen.id="page-footer" at the very bottom of the page.Prefer scrollToElement(...) whenever possible; it's simpler and more readable than manually calculating scroll distances.
Use scrollByAmount(...) and scrollFromOrigin(...) only when you need fine-grained control, such as testing sticky headers, infinite scroll, or scroll-sensitive animations.
Keep your scroll deltas and offsets small and consistent across tests to avoid flaky behavior caused by timing or layout changes.