5
7

More than 5 years have passed since last update.

Python + Seleniumでタスク管理画面のキャプチャを取得する

Last updated at Posted at 2019-04-29

はじめに

 タスク管理ツールとしてMammothProjectというタスク管理アプリを使用しています。
 さて、私の職場では日報が採用されており、やり残したタスクを書く項目があるのですが、なかなかめんどくさいのでタスク管理ツールのキャプチャを貼るようにしました。(後々webスクレイピングにしてもいいかもー)
 そこでやっぱり自動化ですよね。
 想定は

  1. ヘッドレスchromeでMammothProjectにログイン
  2. 自身のタスク一覧画面をキャプチャ

となります。

環境

  • Python 3.7.3
  • mac OS Mojave

後、当コードの実行にあたって以下サイトを参照してSeleniumを実行できる環境を用意しています。
https://qiita.com/memakura/items/20a02161fa7e18d8a693

コード

taskCapture.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import configparser


# chromeを準備
def initDriver():
    # driverを定義
    options = Options()
    options.add_argument('--headless')
    return webdriver.Chrome(options=options)


# 設定ファイル取得
def getIniFile():
    iniFile = configparser.ConfigParser()
    iniFile.read('./config.ini', 'UTF-8')
    return iniFile


# ログイン
def login(driver,iniFile):

    # マンモスプロジェクトにログイン
    driver.get(iniFile.get('settings', 'URL'))
    driver.find_element_by_id('user_email').send_keys(iniFile.get('user', 'name'))
    driver.find_element_by_id('user_password').send_keys(iniFile.get('user', 'password'))
    driver.find_element_by_name('button').click()


# スクリーンショットを撮る。
def screenCaputure(driver, iniFile):

    # なんか狭かったので固定値を設定
    # page_width = driver.execute_script('return document.body.scrollWidth')
    page_width = 1400
    page_height = driver.execute_script('return document.body.scrollHeight')
    driver.set_window_size(page_width, page_height)
    driver.save_screenshot(iniFile.get('user', 'download'))


# メイン処理
def main():

    # chrome画面を準備
    driver = initDriver()

    # 設定ファイル取得
    iniFile = getIniFile()

    # ログイン
    login(driver, iniFile)

    # スクリーンキャプチャ
    screenCaputure(driver, iniFile)

    # 終了
    driver.quit()
    exit()


if __name__ == '__main__':
    main()


config.ini
[settings]
URL : https://app.mmth.pro/mytask#/list

[user]
name     : (ログインするメールアドレス)
password : (ログインするパスワード)
download : (好きなディレクトリ)/タスク.png

結果

できました。
スクリーンショット 2019-04-29 9.53.55.png

画面の幅に改善点はあるものの、その他はいい感じ。
後Pythonを初めて触ってみましたが、手軽にかけていい感じー。

5
7
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
5
7