2

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

Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

3 Answers3

2

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)
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

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)")
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0

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.

Community
  • 1
  • 1
  • Rather than direct to someone else's answer, either provide the relevant components of the code here, or mark this question as a duplicate. – Arya McCarthy May 12 '17 at 17:53