Seleniumでウィンドウの端までスクロールする

seleniumでページ全体のスクリーンショットを撮る場合はこっちのほうがいいです。

nonenull.hatenablog.com

PhantomJSが現役だった頃は簡単に全画面スクリーンショットが撮れたんですが、FirefoxChromeだと難しい。
そこで、全画面スクリーンショットの代わりに画面下までスクロールしてスクリーンショットを繰り返すようにしました。
あとは画像をくっつけると少し不格好ではありますが、全画面スクリーンショットがとれるかと。
メイン部分の処理だけ。
雑に書いたのでそのへんはご勘弁を。

class Main:
    def __init__(self):
        self.driver = GetDriver().get_driver()
        self.screenshot = SaveScreenShot(self.driver)

    def main(self):
        url = "URL"
        self.driver.get(url)

        self.__scroll()

    def __scroll(self):
        SCROLL_PAUSE_TIME = 10

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

        # Window height
        window = self.driver.get_window_rect()
        window_height = window["height"]

        scroll_count = (last_height // window_height) + 1

        height = 0
        count = 0

        for i in range(scroll_count):
            # Screenshot
            self.screenshot.save_screenshot("scroll_test_" + str(count))
            count = count + 1

            height += window_height

            # Scroll down to bottom
            scroll = "window.scrollTo(0, {0});".format(height)
            self.driver.execute_script(scroll)

元ネタっぽいの

stackoverflow.com