Web scraping
How do I scroll to an element in Selenium?
Run scrollIntoView on the element with execute_script, or use ActionChains.scroll_to_element in Selenium 4; centering the element avoids sticky headers covering it.
The direct answer
Selenium usually scrolls on its own when you click, but not always far enough, and never for elements you only want to load or read. The most reliable method is JavaScript: driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", el). Selenium 4 also has a native wheel action, ActionChains(driver).scroll_to_element(el).perform(), which sends real scroll input to the browser. Use the script for precision and the action when you want behavior closer to a user scrolling.
Both methods in code
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By # CSS and XPath strategies
from selenium.webdriver.support.wait import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as conditions
footer_link = driver.find_element(By.CSS_SELECTOR, "a#store-locator")
# option A: script, centered in the viewport
driver.execute_script(
"arguments[0].scrollIntoView({block: 'center', inline: 'nearest'});", footer_link)
# option B: wheel input (Selenium 4.2 and later)
ActionChains(driver).scroll_to_element(footer_link).perform()
Wait(driver, 10).until(conditions.element_to_be_clickable(footer_link)).click()
The wheel API also offers scroll_by_amount(0, 600) for relative scrolling and scroll_from_origin for scrolling inside a specific container. Check the Selenium docs for the bindings in your language; the method names follow the same pattern in Java, C# and JavaScript.
Why centering matters
The default scrollIntoView() aligns the element to the top edge of the viewport. Sites with a sticky header then cover it, and the next click fails with ElementClickInterceptedException: "Other element would receive the click". Passing block: 'center' moves the element out from under fixed bars. If a cookie banner or chat bubble still sits on top, close it first, or scroll a little further with window.scrollBy(0, -120).
Elements that do not exist yet
You cannot scroll to an element that has not been created. On lazy loading lists and infinite feeds, new items appear only after the page is scrolled near the bottom. In that case scroll in steps and check for the element after each step:
import time
for _ in range(30):
hits = driver.find_elements(By.CSS_SELECTOR, "div[data-sku='A-1042']")
if hits:
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", hits[0])
break
driver.execute_script("window.scrollBy(0, window.innerHeight * 0.8);")
time.sleep(0.7)
The short pause gives the site time to fetch the next batch. A better signal is waiting for the item count to grow, as discussed on scraping infinite scroll.
Scrolling inside a container
Modals, side panels and data grids often scroll on their own while the page stays still. scrollIntoView handles nested scroll areas, but scrolling by an amount must target the container: driver.execute_script("arguments[0].scrollTop += 400;", panel). For virtualized grids, rows outside the visible area may not exist in the DOM at all, so read rows as you scroll instead of at the end.
Headless browsers and window size
Headless Chrome starts with a small default window in some setups, which changes what is visible and where sticky elements fall. Set a realistic size such as --window-size=1366,900 so your script behaves the same headless and headful. The page on headless and headful browsers covers other differences.
Common mistakes
- Scrolling then clicking immediately. Smooth scrolling animates. Wait for the element to be clickable before clicking.
- Using sleep as the only wait. Fixed pauses are either too short or too long. Wait on a condition.
- Scrolling the wrong frame. Elements inside an iframe need
switch_to.framefirst. - Treating a CAPTCHA as a scroll bug. If an overlay or challenge appears and nothing behind it responds, scrolling will not help. Solve it first; the Selenium integration shows how to inject a token and continue.
Does scrolling affect detection?
Scroll events are one of many behavior signals some sites record. Jumping instantly between distant points is unusual for a person, while gradual wheel scrolling looks ordinary. Scrolling alone rarely decides whether a session is flagged, since IP reputation and browser fingerprint weigh more, but it adds to the overall picture on sites that score behavior. If a site is sensitive to this, prefer the wheel action and smaller steps, with slightly uneven pauses between them; the question on behaving more like a real user goes further.
Common questions
Does Selenium scroll automatically before clicking?
It tries to bring the element into view, but sticky headers or overlays can still block the click.
What is the difference between scrollIntoView and scroll_to_element?
scrollIntoView is a JavaScript call on the element; scroll_to_element sends wheel input through the WebDriver actions API.
How do I scroll to the bottom of the page?
Run window.scrollTo(0, document.body.scrollHeight) with execute_script, repeating if more content loads.
Why do I get ElementClickInterceptedException after scrolling?
Another element such as a fixed header, banner or modal covers the target. Center the element or close the overlay.
Can I scroll horizontally?
Yes. Use the inline option of scrollIntoView or scroll_by_amount with an x offset.
Keep reading
- SeleniumThe long standing browser automation project built on WebDriver, usable from many languages and scalable through Grid.
- How do I scrape pages with infinite scroll?Call the JSON endpoint the page uses to load more items, or scroll in a browser until no new items arrive.
- Web scraping with Selenium and PythonSelenium scraping that stays stable: explicit waits, locators that survive redesigns, headless Chrome, infinite scroll and stale elements.
- SeleniumThe reference Selenium setup in Python and Java: explicit waits, reCAPTCHA v2 and Turnstile tokens, callbacks, retries and Grid scaling.