Scroll an Element to the Centre of the Viewport
Avoid clicks blocked by sticky headers and cookie bars by scrolling the target to the middle of the screen, with instant behaviour so the next click is safe.
Problem
When WebDriver clicks an element that is off-screen, it scrolls the element into view first, but often only just far enough, leaving it at the very top or bottom edge. A sticky header or a fixed cookie bar sits on top of it, and the click fails with ElementClickInterceptedException: Other element would receive the click. The same happens with Actions.scrollToElement, which aligns the element with the bottom of the viewport.
Solution
Scroll the element to the centre of the viewport with scrollIntoView({ block: 'center' }) before interacting. Use behavior: 'instant' so the scroll has finished by the time the script returns, even if the site sets scroll-behavior: smooth.
public static WebElement scrollToCenter(WebDriver driver, WebElement el) { ((JavascriptExecutor) driver).executeScript( "arguments[0].scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});", el); return el;}
WebElement save = driver.findElement(By.id("save-preferences"));scrollToCenter(driver, save);new WebDriverWait(driver, Duration.ofSeconds(5)) .until(ExpectedConditions.elementToBeClickable(save)) .click();def scroll_to_center(driver, el): driver.execute_script( "arguments[0].scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});", el) return el
save = driver.find_element(By.ID, "save-preferences")scroll_to_center(driver, save)WebDriverWait(driver, 5).until(EC.element_to_be_clickable(save)).click()async function scrollToCenter(driver, el) {await driver.executeScript( "arguments[0].scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});", el);return el;}
const save = await driver.findElement(By.id('save-preferences'));await scrollToCenter(driver, save);await driver.wait(until.elementIsEnabled(save), 5000);await save.click();public static IWebElement ScrollToCenter(IWebDriver driver, IWebElement el){ ((IJavaScriptExecutor)driver).ExecuteScript( "arguments[0].scrollIntoView({block: 'center', inline: 'center', behavior: 'instant'});", el); return el;}
var save = driver.FindElement(By.Id("save-preferences"));ScrollToCenter(driver, save);save.Click();Why It Works
Sticky headers live at the top of the viewport and cookie bars or chat widgets at the bottom. The vertical centre is the one place that is almost never covered. scrollIntoView scrolls every scrollable ancestor as needed, so it works for elements inside scrollable panels and modals as well as the main page. behavior: 'instant' overrides any CSS scroll-behavior: smooth; without it the scroll animates and the following click can still race it.
Options Compared
| Method | Where the element ends up | Notes |
|---|---|---|
Implicit scroll on click() | Edge of viewport (browser-dependent) | Fine unless something is fixed at that edge |
Actions.scrollToElement(el) | Bottom edge | W3C wheel action; does not centre |
scrollIntoView(true) | Top edge | Lands under sticky headers |
scrollIntoView({block: 'center'}) | Centre | Best default for clicks |
Actions.scrollFromOrigin(...) with offset | Wherever you compute | For testing real wheel-scroll behaviour |
Gotchas
- Put it in your click helper. Centring before every click in a base page method costs one script call and removes a whole category of intercepted clicks.
behavior: 'instant'is the standard name. Very old browsers only knew'auto', which also respects the CSS setting. Current Chrome, Edge, Firefox and Safari all support'instant'.- Scroll-triggered UI. Some headers appear when you scroll up and hide when you scroll down. Centring an element above the current position can reveal the header right over it; if that happens, scroll a little further with
window.scrollBy. - Not a real user scroll.
scrollIntoViewdoes not fire wheel events. If the behaviour under test depends on them (parallax, lazy loading on wheel), use the Actions wheel API instead. - If the click is still intercepted, read the exception message: it names the element that would receive the click. That is often an overlay you should dismiss or wait out (see Wait for a Loading Spinner to Disappear), not a scrolling problem.