Careeroza
Scrolling with JavaScript
Common Scroll Scripts
js.executeScript("window.scrollBy(0, 600);"); // down 600 px
js.executeScript("window.scrollTo(0, document.body.scrollHeight);"); // to the bottom
js.executeScript("arguments[0].scrollIntoView({block: 'center'});", driver.findElement(By.id("reviews")));
Long y = (Long) js.executeScript("return Math.round(window.scrollY);");
System.out.println("Scrolled to y=" + y);Example
Output:
Run JsScrollTest.main()Example output
Scrolled to y=2140
Why block center
scrollIntoView() with the default puts the element at the very top, where ShopEase's sticky header covers it, and the next click fails with ElementClickInterceptedException. {block: 'center'} keeps it in the middle of the screen.