LoginSignup
41
42

More than 3 years have passed since last update.

【Python】Seleniumでスクリーンショットを撮る

Last updated at Posted at 2019-10-23
  • seleniumで遷移したウェブページのスクショを撮った時のメモ
  • 以下のコードと同ディレクトリ内にimageという名前のディレクトリを作成し実行してください
import os
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# File Name
FILENAME = os.path.join(os.path.dirname(os.path.abspath(__file__)), "image/screen.png")

# set driver and url
driver = webdriver.Chrome('./chromedriver')
url = 'https://www.rakuten.co.jp/'
driver.get(url)

# get width and height of the page
w = driver.execute_script("return document.body.scrollWidth;")
h = driver.execute_script("return document.body.scrollHeight;")

# set window size
driver.set_window_size(w,h)

# Get Screen Shot
driver.save_screenshot(FILENAME)

# Close Web Browser
driver.quit()

撮られたスクショがこのようにディレクトリimage内に保存されています.

screen.png

GitHubをクローンしてexe.shを実行するのもありです.

41
42
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
41
42