LoginSignup
1
4

More than 3 years have passed since last update.

Chrome画面のスクリーンショットを取る方法(途中で切れるのを防ぐ)

Posted at

背景

Seleniumを使って自動でスクリーンショットを取ろうとしたのですが、いくら大きいサイズを指定しても、画面サイズで切れてしまってスクロールの下の方が取れずに苦労したので、その備忘録です。

使用したもの

chromedriver(使用しているchromeのバージョンにあったもの)
selenium
python

対応法

optionsでheadless付ければOKでした。

screenshot.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

if __name__ == "__main__":

    # 切れないスクリーンショットのために必要な設定
    options = Options()
    options.add_argument('--headless')
    driver = webdriver.Chrome(options=options)

    # サイズを広めに指定しておく
    driver.set_window_size(1400, 2000)

    # キャプチャ先の指定
    driver.get(r'https://ja.wikipedia.org/wiki/Qiita')

    # キャプチャ実施。保存ファイルを指定。
    driver.save_screenshot('screenshot.png')

    driver.close()

考察

optionsでheadlessを付けると、Chromeが立ち上がることがなくなります。
つけない場合にスクショが切れてしまうのは、おそらくChromeが立ち上がった際にwindow幅に合わせて縦・横が微修正されるからなのかなぁ。。。と。多分そう。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4