javascriptselenium-webdriverjs-scrollintoviewjs-scrolltojs-scrollby

What is the difference between the different scroll options?


I have tried a few ways of adding scrolling to tables, but just one of them works correctly. What is the difference between them?

First:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", Element);

Second:

WebElement element1 = driver.findElement(By.id("scrolled_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element1);

Third:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");

Fourth:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Solution

  • Element.scrollIntoView()

    Element.scrollIntoView() method scrolls the element on which it's called into the Viewport of the browser window.


    Window.scrollBy()

    window.scrollBy() method scrolls the document in the current window by the given amount.


    Window.scrollTo()

    Window.scrollTo() method scrolls to a particular set of coordinates in the document.