'How can i find total height of page by scrolling

I've been trying to find total height of page whose data is rendered as I scroll down. I wrote below code but it always results in different heights as an output. My goal is to find a fixed height value.

    def calculate_total_height(self):
        self.total_height = int(self.driver.execute_script(
            "return document.body.scrollHeight"))
        while True:
            self.driver.execute_script(
                "window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(10)
            new_height = self.driver.execute_script(
                "return document.body.scrollHeight")
            if new_height == self.total_height:
                break
            self.total_height = new_height

How can i reach my goal?



Solution 1:[1]

Since the page you are trying to measure it's height is scrollable, by scrolling the page it's height will increase!
By scrolling the page you are loading more elements there, these elements were not existing there before so this is actually increasing the page height.
Only in case the page is loading all it's elements even before scrolling so that scrolling will not cause any actual changes on the page the page height will not change by scrolling.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prophet