LoginSignup
23
29

More than 5 years have passed since last update.

SeleniumとPythonで定期実行処理を作ってみた

Last updated at Posted at 2015-09-07

アニメネタ満載のブログしか書いたことがない僕が、
備忘録も兼ねて投稿します。

そもそも定期実行処理を作ろうとしたきっかけは、
とあるWebサイトで毎週、毎週、同じことをやってるけど、自動でやってくれない?
というお願いからです。

ということで

目的

  • とあるWebサイト上で同じことを定期的に行う

定期実行処理に利用するもの

  • CentOS release 6.7 (さくらのVPS)
  • Selenium IDE
  • Python
  • Python用Selenium
  • Firefox
  • Xvfb
  • crontab

Selenium IDEでPython用テストケース作成(WindowsもしくはMac等)

  1. Selenium IDEをFirefoxにインストール
  2. Selenium IDEでWebサイト上の自動で行いたい手順を記録
  3. テストケースをエクスポート => Python 2 / unittest / WebDriver
  4. 保存したテストケースをCentOSにアップロード

定期実行処理の環境構築(CentOS)

Pythonのパッケージ管理システムインストール
yum install python-pip
PythonのSeleniumインストール
pip install selenium
Firefoxインストール
yum install firefox
Xvfbインストール
yum install xorg-x11-server-Xvfb
Xvfb自動起動サービス作成
vi /etc/init.d/xvfb
/etc/init.d/xvfb
#!/bin/bash
#
# chkconfig: - 91 35
# description: Xvfb

# Source function library.
. /etc/init.d/functions

# Xvfb define
readonly XVFB=/usr/bin/Xvfb
readonly XVFB_STATUS=":1 -screen 0 1366x768x24"
readonly XVFB_PID_FILE=/var/run/xvfb.pid
readonly XVFB_SERVICE=$"Xvfb"

retval=0

start() {
    if [ -e ${XVFB_PID_FILE} ]; then
        action $"Starting ${XVFB_SERVICE}: " /bin/false
        echo "${XVFB_SERVICE} は既に起動しています。"
    else
        action $"Starting ${XVFB_SERVICE}: " /bin/true
        ${XVFB} ${XVFB_STATUS} > /dev/null 2>&1 &
        echo $! > ${XVFB_PID_FILE}
    fi
}

stop() {
    if [ -e ${XVFB_PID_FILE} ]; then
        action $"Stopping ${XVFB_SERVICE}: " /bin/true
        pid=`cat ${XVFB_PID_FILE}`
        test ! -z $pid && kill $pid && rm -f ${XVFB_PID_FILE}
    else
        action $"Stopping ${XVFB_SERVICE}: " /bin/false
        echo "${XVFB_SERVICE} は起動していません。"
    fi
}

status() {
    if [ -e ${XVFB_PID_FILE} ]; then
        echo "${XVFB_SERVICE} (pid `cat ${XVFB_PID_FILE}`) を実行中..."
    else
        echo "${XVFB_SERVICE} は起動していません。"
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 1
        start
        ;;
    status)
        status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        retval=1
esac

exit ${retval}
Xvfb自動起動サービス設定
chmod 755 /etc/init.d/xvfb
chkconfig --add xvfb
chkconfig --level 3 xvfb on
Xvfb再起動
/etc/init.d/xvfb restart
定期実行処理作成
crontab -e
cron
# cron実行結果確認用メールアドレス指定
MAILTO=xxxxx@gmail.com
# /home/kotanbo/test.py 部分はアップロードしたテストケースを指定
# 下記は日曜9時に定期実行
0 9 * * 0 export DISPLAY=localhost:1.0; python /home/kotanbo/test.py
cron再起動
/etc/init.d/crond restart

参考

Linux CUI環境でruby + selenium-webdriver
Xvfb 起動スクリプト
centosでseleniumサーバー自動起動

追記

2017/04/27

最新のfirefoxやseleniumでは恐らく上記内容で動作しません。
新しく環境構築した内容を記載したので、こちらもどうぞ。
 => CentOS7とSeleniumとPythonとChromeで定期実行処理を作ってみた

23
29
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
23
29