How to get current scroll height in selenium python

Viewed 2932

I want to get current scroll height in selenium python Such as if i have scrolled in the middle of stack overflow, so i want the current scroll height in using selenium in python Thanks I have tried this

height =  browser.execute_script("return document.body.scrollHeight")
3 Answers

Instead try this :

height = browser.execute_script("return document.documentElement.scrollHeight")

To get a page scroll height you can use below code

height = int(driver.execute_script("return document.documentElement.scrollHeight"))

But if you want to get the current position of scroll height, than you have to use.

totalScrolledHeight = driver.execute_script("return window.pageYOffset + window.innerHeight")

Reference: Selenium Scroll Current Position

This worked for me:

element = driver.find_element_by_tag_name("body")

scroll_height = element.get_attribute("scrollHeight")
Related