How to scroll to an element in Selenium
When a target sits below the fold, Selenium can still locate it, but clicks and reads may fail or return stale positions.
The reliable pattern is to locate the WebElement first, then hand it to execute_script with a JavaScript scroll expression so the browser moves the element into view before you act on it.
The core pattern
Selenium exposes execute_script for running JavaScript in the page context. You pass a JavaScript string plus any arguments the script needs. For scrolling, the string is a scrollIntoView call and the argument is the WebElement you already located with any supported Selenium selector. The browser resolves arguments[0] to that element and scrolls it into the viewport.
Argument and control map
Two inputs drive the call: the JavaScript string and the element reference. The string uses arguments[0] as a placeholder; the element you pass second becomes that placeholder. If you pass no element, arguments[0] is undefined and the script throws. If you pass a locator string instead of a WebElement, the same error occurs. The return value of scrollIntoView is not needed, so execute_script's return can be ignored.
Worked example: scrolling to a footer
The original example navigates to a site, locates the footer by tag name, and scrolls it into view. The same structure works for any element: swap the locator for an id, class, CSS selector, or XPath that matches your target. The JavaScript expression stays the same because it operates on whatever element you pass.
Checklist before you scroll
- Confirm the element exists in the DOM at the time you locate it. 2) Confirm it is not inside a nested scroll container that scrollIntoView may not reach the way you expect. 3) Confirm no overlay or sticky header will cover the element after scrolling. 4) Confirm you are passing the WebElement, not its locator. 5) Confirm the page has finished loading the element before you scroll.
Failure distinctions
Three different failures look similar. NoSuchElementException means the locator did not match anything. A JavaScript error about arguments[0] means the element argument was missing or not a WebElement. An ElementClickInterceptedException after a successful scroll usually means something is covering the element, not that the scroll failed. Diagnose which one you have before changing the scroll code.
Frequently asked questions
Can I scroll to an element without JavaScript?
The pattern shown here relies on execute_script and a JavaScript scroll expression. Selenium's action APIs can also move the viewport, but the captured approach uses execute_script with scrollIntoView.
Why does arguments[0] fail?
arguments[0] refers to the first argument passed after the script string. If you pass nothing, or pass a locator string instead of a WebElement, the JavaScript cannot resolve the element and the call fails.
Does scrollIntoView work for any element?
It works on any WebElement you can locate with a supported Selenium selector. The example uses a footer tag, but the same expression applies to any element reference you pass in.