scroll up the page to the top in selenium
Asked Answered
I

8

22

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

Insular answered 15/4, 2016 at 12:56 Comment(1)
Python: driver.execute_script("window.scrollTo(0, 0)")Reposit
S
33

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); 
Suspensoid answered 15/4, 2016 at 13:13 Comment(1)
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"}Auburn
S
7

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();
Sedate answered 12/6, 2020 at 19:3 Comment(0)
A
3

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

((JavascriptExecutor) driver)
    .executeScript("window.scrollTo(0, -document.body.scrollHeight)");
Appellate answered 22/1, 2019 at 8:22 Comment(0)
C
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)");
Consciousness answered 22/7, 2019 at 14:20 Comment(0)
L
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)");
Lichee answered 15/4, 2016 at 13:16 Comment(1)
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 upInsular
S
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);

Stephenstephenie answered 29/12, 2019 at 8:47 Comment(0)
C
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);
Cordite answered 21/2, 2023 at 5:6 Comment(0)
F
0

Scrolling is done in the browser and usually has an animation involved. If you need instant scrolling, for example to take a screenshot, use this:

window.scrollTo({behavior: "instant", top: 0, left: 0});

From Python Selenium driver:

driver.execute_script('window.scrollTo({behavior: "instant", top: 0, left: 0})')
Frill answered 18/10, 2023 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.