18

How to scroll the webpage to the top of the page.

I know scrolling the page to the bottom is:

window.scrollTo(0,document.body.scrollHeight)

just like that is it possible to scroll the page to the top

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Pala Bhaskar
  • 311
  • 1
  • 2
  • 9

7 Answers7

30

To scroll to the top of the page, just scroll to the 0, 0:

window.scrollTo(0, 0);

Or, as an alternative option, you can scroll into view of the header element (or some other element on top):

WebElement element = driver.findElement(By.tagName("header"));

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView();", element); 
Shivam Mishra
  • 1,731
  • 2
  • 11
  • 29
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    where did you get the window instance? and for the second approach I'm getting: Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"header"} – White_King Feb 02 '21 at 09:32
6

Use action class, as some UI frameworks don't work well with JavaScript scrollTO

actions.sendKeys(keys.Home).build().perform();
actions.sendKeys(keys.END).build().perform();
David Buck
  • 3,752
  • 35
  • 31
  • 35
Frozen
  • 69
  • 1
  • 1
3

This solution also works correctly, I've checked it:

((JavascriptExecutor) driver)
    .executeScript("window.scrollTo(0, -document.body.scrollHeight)");
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
outlaw1988
  • 41
  • 7
2

To scroll to the top of the page

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

To scroll to the end of the page

((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
alkakaushal
  • 144
  • 1
  • 5
1

yes you can try as below

Way one - Scrolling to bottom of a page

driver.navigate().to(URL);
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Way two - Scrolling to an element on a page

driver.navigate().to(URL);
WebElement element = driver.findElement(By.id("id"));
        ((JavascriptExecutor) driver).executeScript(
                "arguments[0].scrollIntoView();", element);

Way 3 -Scrolling by coordinates

 driver.navigate().to(URL);
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • 2
    But I want to scroll the page to the top. Here just like (Way one - Scrolling to bottom of a page), I want to scroll the page to the up – Pala Bhaskar Apr 15 '16 at 14:23
1

simple way for the top :webDriver.FindElement(By.TagName("body")).SendKeys(Keys.Home); and for the bottom: webDriver.FindElement(By.TagName("body")).SendKeys(Keys.End);

arman
  • 649
  • 1
  • 10
  • 27
0

You can use the below solutions. x-pixels is the number at the x-axis, it moves to the left if the number is positive and it moves to the right if the number is negative .y-pixels is the number at the y-axis, it moves to the down if a number is positive and it moves to the up if the number is in negative .

Scroll Method:

executeScript("window.scrollBy(x-pixels,y-pixels)");

Sample Code:

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

Or you can scroll by using element present on the top of page

WebElement element = driver.findElement(By.linkText("home"));  
js.executeScript("arguments[0].scrollIntoView();", element);
Suraj
  • 317
  • 4
  • 7