how to scroll to the bottom of the page with selenium python

Viewed 27

I have tried driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") after the page has loaded to no avail. It simply does nothing.

Why isn't it working? Is there another method I can use.

3 Answers

scrollTo is usually the preferred way but not possible on every site. Alternatively you can use this:

from selenium.webdriver.common.keys import Keys
elem = driver.find_element(By.TAG_NAME, "html")
elem.send_keys(Keys.END)

However, I would much prefer requests instead of selenium.

There are several ways to scroll the page with Selenium.
Additionally to

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

You can try

from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)

But the most powerful way that normally works even when the previously methods will not is:
Locate some element out of the visible screen, maybe on the bottom of the page and apply on it the following

bottom_element = driver.find_element(By.XPATH, bottom_element_locator)
bottom_element.location_once_scrolled_into_view

This originally intend to return you coordinates (x, y) of element on page, but also scroll down right to target element

Another way to scroll to bottom of page is to emulate the CTRL + END key combination

from selenium.webdriver.common.keys import Keys

driver.find_element_by_css_selector('body').send_keys(Keys.CONTROL + Keys.END)
Related