Skip to content

Instantly share code, notes, and snippets.

@YoshihitoAso
Created March 23, 2016 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshihitoAso/59d1967b0de8f4448f51 to your computer and use it in GitHub Desktop.
Save YoshihitoAso/59d1967b0de8f4448f51 to your computer and use it in GitHub Desktop.
Seleniumで画面のスクリーンショットを取る
from selenium import webdriver
from xvfbwrapper import Xvfb
import sys
#----------------------------------------------------------------
# Selenium Screenshot
#
# Copyright 2016 Yoshihito Aso
#----------------------------------------------------------------
class SeleniumScreenshot(object):
def __init__(self, _width, _height):
# 日本語を適用
fp = webdriver.FirefoxProfile()
fp.set_preference('intl.accept_languages', 'ja-JP, ja')
self.xvfb = Xvfb(width=_width, height=_height)
self.xvfb.start()
# webdriverを作成
self.browser = webdriver.Firefox(firefox_profile=fp)
def capPage(self, page, file_name):
# 2秒待つ
self.browser.implicitly_wait(2)
# ページに遷移
self.browser.get(page)
# キャプチャ
self.browser.save_screenshot(file_name)
# ブラウザを閉じる
self.browser.close()
def endCapture(self):
self.xvfb.stop()
self.browser.quit()
if __name__ == '__main__':
# 引数のチェック
argvs = sys.argv
argc = len(argvs)
if (argc != 2):
print('Usage: # python %s out_file' % argvs[0])
quit()
out_file = argvs[1]
capture = SeleniumScreenshot(1280, 720)
capture.capPage("https://www.google.co.jp", out_file)
capture.endCapture()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment