How to scroll down the page till bottom(end page) in the Selenium WebDriver
Asked Answered
F

3

29

I need to scroll down page till end in the Selenium WebDriver. I tried to scroll down the page by using the following code snippet:

JavascriptExecutor jse6 = (JavascriptExecutor) driver;
jse6.executeScript("window.scrollBy(0,250)", "");

It's being scrolled but I need to scroll down till end page.

Flavoring answered 23/3, 2017 at 17:26 Comment(5)
Possible duplicate of Page scroll up or down in Selenium WebDriver (Selenium 2) using javaInobservance
@Inobservance I believe this is a distinct question, since it's asking how to scroll to the absolute end of the page, which is different from just scrolling up or down.Lory
@JonathanBenn If you look at all the various answers on that question, several of them show how to scroll down to the end of the page, e.g. user3472488's answer and more.Inobservance
@Inobservance I agree that the answers have elements in common, but the question is different. :) I guess my point is that someone searching for how to scroll down to the bottom of the page will easily find this question, but not necessarily the one to which you've linked. Hence, this question has value in and of itself.Lory
@JonathanBenn You can word it however you like but the essential question is exactly the same and has the same answer. The question I linked has SEVERAL answers that you can choose from. I'm not sure why we are having this discussion 2 years after this question was asked and my comment was originally made...Inobservance
Z
63

We have to use JavascriptExecutor

To scroll using coordinate

((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");

To scroll till end of the page

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

To scroll till any element

((JavascriptExecutor) driver).executeScript(
            "arguments[0].scrollIntoView();", element);
Zingaro answered 23/3, 2017 at 17:45 Comment(2)
Please don't answer questions that have already been answered. Mark or flag them as duplicates instead so they can be cleaned up.Inobservance
How to scroll to the bottom without given a element xpath or location?Cervantes
D
7

do it with python,

import time

time.sleep(2)
drive.execute_script("window.scrollTo(0, document.body.scrollHeight)")

based on @shubham bansal's

Duomo answered 12/5, 2022 at 7:37 Comment(0)
V
2

For this you can take the xpath of any object at the end of the page manually.And use the below code.

WebElement lastElement = 
driver.findElement(By.xpath("//a[@title='org.apache.spark download']"));
int y = lastElement.getLocation().getY();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,"+y+")");
Thread.sleep(3000);
Viaduct answered 4/4, 2017 at 11:59 Comment(1)
Not only Thread.sleep() is unnecessary, you are adding a 3 second pause? Terrible answer.Enyo

© 2022 - 2024 — McMap. All rights reserved.