Home > Net >  how to scroll to the bottom of the page with selenium python
how to scroll to the bottom of the page with selenium python

Time:09-21

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.

CodePudding user response:

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.

CodePudding user response:

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

  • Related