Posted on
  • self._scroll_pause_time is a member variable defined elsewhere.
    def _scroll_to_bottom(self, driver):
        '''
        credit to:
        1. https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python
                                                                                                        #answer-43299513
        2. https://stackoverflow.com/questions/15932650/body-scrollheight-doesnt-work-in-firefox#answer-46905244
        :param driver: chromedriver
        :return: None
        '''

        # Get scroll height
        last_height = driver.execute_script(
            "return document.body.scrollHeight || document.documentElement.scrollHeight;"
        )

        while True:
            # Scroll down to bottom
            driver.execute_script(
                "window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);"
            )

            # Wait to load page
            time.sleep(self._scroll_pause_time)

            # Calculate new scroll height and compare with last scroll height
            new_height = driver.execute_script(
                "return document.body.scrollHeight || document.documentElement.scrollHeight;"
            )
            if new_height == last_height:
                break
            last_height = new_height

References

    1. https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python#answer-43299513
    2. https://stackoverflow.com/questions/15932650/body-scrollheight-doesnt-work-in-firefox#answer-46905244

Leave a Reply

Your email address will not be published. Required fields are marked *