'How to scroll smoothly with Selenium WebDriver and/or Sikuli in Java

As part of a test suite measuring FPS for a web application I need to perform a smooth scroll of the web page. That is, the same smoothness as when a user grabs the scroll bar and moves it with the mouse.

So far I have tried by using simulating key presses with sikuli, i.e. pressing the arrow up/down keys multiple times to scroll the whole page. I have also tried using a Javascript approach:

public void scrollSmooth(int durationOfScroll){
    long timeWhenStarting = System.currentTimeMillis() / 1000L;
while (System.currentTimeMillis() / 1000L - timeWhenStarting < durationOfScroll) {
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,10)", "");
    }
}

Both these approaches fails to fulfill their purpose, as they both generate a step-by-step scroll, which is not suitable when I simultaneously want to measure the FPS (e.g. the smoothnes of the page when scrolling).



Solution 1:[1]

The solution was a lot more simple than expected. Instead of using a time-based condition for the loop I tried the following:

public void scrollSmooth(){
    for(int i=0;i<6000;i++) {
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,1)", "");
    }
}

This works well, with the small downside that I cannot specify the length (in time) of the scroll, only the actual pixels to be scrolled.

Solution 2:[2]

Two methods scrollDown and scrollUp,Hope it 'll help. :)

 /**
 * scrollDown() method scrolls down the page.
 *
 * @return void
 */
public void scrollDown(WebDriver driver) {
    try {
        int i=0;
        for(;i<=30;i++) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
        for(;i>0;i--) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
    } catch (WebDriverException wde) {
    } catch (Exception e) {
    }
}

/**
 * scrollUp() method scrolls up the page.
 *
 * @return void
 */
public void scrollUp(WebDriver driver) {
    try {
        int i=0;
        for(;i>-30;i--) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
        for(;i<0;i++) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
    } catch (WebDriverException wde) {
    } catch (Exception e) {
    }
}

Solution 3:[3]

html = driver.find_element(By.XPATH,'//body')
total_scroled = 0
page_height = driver.execute_script("return document.body.scrollHeight")
while total_scroled < page_height:
    html.send_keys(Keys.PAGE_DOWN)
    total_scroled += 400
    time.sleep(.5)

Solution 4:[4]

Here is a version for Selenium with Python -

from selenium import webdriver
import chromedriver_autoinstaller
from time import sleep

chromedriver_autoinstaller.install()
driver = webdriver.Chrome()

def scroll_down(driver):
    page_height = driver.execute_script("return document.body.scrollHeight")
    total_scrolled = 0
    for i in range(page_height):
        driver.execute_script(f'window.scrollBy(0,{i});')
        total_scrolled += i
        if total_scrolled >= page_height/2:
            last_no = i
            break
            
    for i in range(last_no, 0, -1):
        driver.execute_script(f'window.scrollBy(0,{i});')


def scroll_up(driver):
    page_height = driver.execute_script("return document.body.scrollHeight")
    total_scrolled = 0
    for i in range(0, -page_height, -1):
        driver.execute_script(f'window.scrollBy(0,{i});')
        total_scrolled += i
        if total_scrolled <= -page_height/2:
            last_no = i
            break
    for i in range(last_no, 0):
        driver.execute_script(f'window.scrollBy(0,{i});')

scroll_down(driver)
sleep(2)
scroll_up(driver)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Agent Shoulder
Solution 2 OptimusPrime
Solution 3 Souvik98
Solution 4 Mahmudur Rahman Shovon