How to scroll a page to the end

I have tried to do this:

driver_1.execute_script("window.scrollTo(0, document.body.scrollHeight);")

but it does nothing, so I made a loop to scroll the page by steps:

initial_value = 0
end = 300000
for i in xrange(1000,end,1000):
    driver_1.execute_script("window.scrollTo(" + str(initial_value) + ', ' +  str(i) + ")")
    time.sleep(0.5)
    initial_value = i
    print 'scrolling >>>>'

It kinda works, but I don't know how long is a a given page, so I have to put a big number as the max height, that gives me two problems. First is that even a big number couldn't be large enought to scroll some pages and second one is that if the page is shorter than that limit a loss quite a lot time waiting for the script to finish when is doing nothing

Upvotes: 2

Views: 1862

Answers (3)

Irina Fumigenna
Irina Fumigenna

Reputation: 1

Hey I found another solution that worked perfectly for me. Check this answer here. Also this implementation: driver.find_element_by_tag_name("body").send_keys(Keys.END) does not work for pages that that use infinite scrolling design.

Upvotes: 0

Florent B.
Florent B.

Reputation: 42528

To scroll the page to the end, you could simply send the END key:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/search?tab=newest&q=selenium")
driver.find_element_by_tag_name("body").send_keys(Keys.END)

You could also scroll the full height :

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/search?tab=newest&q=selenium")
driver.execute_script("window.scrollBy(0, document.documentElement.scrollHeight)")

Upvotes: 0

alecxe
alecxe

Reputation: 473873

You need something to rely on, some indicator for you to stop scrolling. Here is an example use case when we would stop scrolling if more than N particular elements are already loaded:

Similar use case:

FYI, you may have noticed an other way to scroll to bottom - scrolling into view of a footer:

footer = driver.find_element_by_tag_name("footer")
driver.execute_script("arguments[0].scrollIntoView();", footer)

Upvotes: 2

Related Questions