Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 273
bezzoon Avatar asked Sep 12 '25 19:09

bezzoon


1 Answers

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

like image 139
Prophet Avatar answered Sep 14 '25 11:09

Prophet