LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

【社内作業向け_当日作業報告】seleniumでリモートワーク申請自動化【headless】

Last updated at Posted at 2020-03-31

※headlees化しているのでバックグラウンドで実行されます。
chromeブラウザすら立ち上がらないので、何も気にせずPCを操作できます。

ソースコード

【社内作業向け_当日作業報告】seleniumでリモートワーク申請自動化【非headless】と違うのは「initメソッド」と「login_googleメソッド」のみで、あとは全て同じです。

""" リモートワーク当日作業報告
"""
import chromedriver_binary
import time
from datetime import datetime
from selenium import webdriver


class RakumoRemoteWork:
    """ Rakumoリモートワーク申請自動化
    """
    def __init__(self, *args, **kwargs):
        self.year = datetime.now().year
        self.month = datetime.now().month
        self.day = datetime.now().day

        self.schedule_work = (\
            '【作業内容】\n'
            '10:00~12:00 開発\n'
            '12:00~13:00 昼食\n'
            f'13:00~{datetime.now().hour}{datetime.now().minute} 開発'\
        )

        # chromeでrakumoを開く(headlessなのでchromeは立ち上がらない)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')  # headless化のオプション
        self.driver = webdriver.Chrome(options=chrome_options)
        self.driver.get('https://a-rakumo.appspot.com/login')

    def main(self):
        self.login_rakumo()
        self.login_google()
        self.rakumo_workflow()
        self.rakumo_kintai()
        self.driver.quit()  # 終了

    def login_rakumo(self):
        """ rakumoにログインする
        """
        search_box = self.driver.find_element_by_name("domain")
        search_box.send_keys('【ここに会社指定のrakumoID】')
        search_box.submit()

    def login_google(self):
        """ googleアカウントでログインする
        """
        # メールアドレス入力
        time.sleep(5)
        input_id = self.driver.find_element_by_id("Email")
        input_id.send_keys('【ここにGoogleアカウントのメールアドレス】')

        # 次へボタンクリック
        self.driver.find_element_by_name("signIn").click()

        time.sleep(5)

        # パスワード入力
        input_id = self.driver.find_element_by_id("Passwd")
        input_id.send_keys('【ここにGoogleアカウントのパスワード】')

        # 次へ
        self.driver.find_element_by_name("signIn").click()

    def rakumo_workflow(self):
        """ ワークフローからリモートワーク申請を行う
        """
        # ワークフローリンク押下でワークフロー画面を開く
        time.sleep(15)
        href_workflow = self.driver.find_element_by_xpath('//a[@href="/workflow"]')
        href_workflow.click()

        # 別タブで開かれるので、driverのタブをそのタブに切り替える
        time.sleep(10)
        tabs = self.driver.window_handles
        self.driver.switch_to.window(tabs[1])

        # 申請するボタンクリック
        self.driver.find_element_by_id('dijit_form_ComboButton_0_label').click()

        # 申請書クリック
        time.sleep(3)
        self.driver.find_elements_by_class_name('blockContent')[0].click()

        # 稟議・報告関連クリック
        time.sleep(3)
        self.driver.find_elements_by_class_name('blockContent')[3].click()

        # 社内リモート作業日報クリック
        time.sleep(3)
        self.driver.find_elements_by_class_name('blockContent')[1].click()

        # 件名入力
        time.sleep(3)
        input_subject = self.driver.find_element_by_name('item_1')
        input_subject.clear()
        input_subject.send_keys(f'リモート作業日報({self.year}{self.month}{self.day}日)')

        # 予定作業内容
        input_schedule_work = self.driver.find_element_by_name('item_3')
        input_schedule_work.clear()
        input_schedule_work.send_keys(self.schedule_work)

        # 確認ボタン押下
        self.driver.find_element_by_id('wizardNextBtn').click()

        # 申請ボタン押下
        self.driver.find_element_by_id('wizardSaveBtn').click()

    def rakumo_kintai(self):
        """ 勤怠画面の備考欄に「リモート作業」と入力する
        """
        # キンタイリンククリックで勤怠画面に遷移
        time.sleep(15)
        href_workflow = self.driver.find_element_by_xpath('//a[@href="/attendance"]')
        href_workflow.click()

        # これも同様に別タブで開かれるので、driverのタブを切り替える
        time.sleep(20)
        tabs = self.driver.window_handles
        self.driver.switch_to.window(tabs[2])

        # # 退勤ボタンクリック
        # buttons = self.driver.find_elements_by_tag_name('fasti-button')
        # for button in buttons:
        #     if button.text == '退勤':
        #         button.click()
        #         break

        # 備考に「リモート作業」と入力して保存ボタンクリック
        self.driver.find_elements_by_class_name('note')[self.day - 1].click()
        note = self.driver.find_element_by_tag_name('textarea')
        note.clear()
        note.send_keys('リモート作業')
        save_buttons = self.driver.find_elements_by_tag_name('fasti-button')
        for button in buttons:
            if button.text == '保存':
                button.click()
                break


if __name__ == "__main__":
    RakumoRemoteWork().main()

chromedriver_binaryのインストール

chromeのバージョン確認

image.png

pip インストール

上記で確認したchromeのバージョンを指定する

$ pip3 install chromedriver-binary==80.0.3987.149

不正なバージョンだった場合はインストール可能なものが一覧表示されるので、メジャーバージョンが一致するものをコピーして再度pip installする。
80.0.3987.149のメジャーバージョンは80

Collecting chromedriver-binary==80.0.3987.149
  ERROR: Could not find a version that satisfies the requirement chromedriver-binary==80.0.3987.149 (from versions: 2.29.1, 2.31.1, 2.33.1, 2.34.0, 2.35.0, 2.35.1, 2.36.0, 2.37.0, 2.38.0, 2.39.0, 2.40.1, 2.41.0, 2.42.0, 2.43.0, 2.44.0, 2.45.0, 2.46.0, 70.0.3538.16.0, 70.0.3538.67.0, 70.0.3538.97.0, 71.0.3578.30.0, 71.0.3578.33.0, 71.0.3578.80.0, 71.0.3578.137.0, 72.0.3626.7.0, 72.0.3626.69.0, 73.0.3683.20.0, 73.0.3683.68.0, 74.0.3729.6.0, 75.0.3770.8.0, 75.0.3770.90.0, 75.0.3770.140.0, 76.0.3809.12.0, 76.0.3809.25.0, 76.0.3809.68.0, 76.0.3809.126.0, 77.0.3865.10.0, 77.0.3865.40.0, 78.0.3904.11.0, 78.0.3904.70.0, 78.0.3904.105.0, 79.0.3945.16.0, 79.0.3945.36.0, 80.0.3987.16.0, 80.0.3987.106.0, 81.0.4044.20.0, 81.0.4044.69.0)
ERROR: No matching distribution found for chromedriver-binary==80.0.3987.149

この場合はメジャーバージョンが一致しているのは「80.0.3987.16.0」と「80.0.3987.106.0」なので、どちらかのバージョンを指定してpip install。

ソースコードにchromedriver-binaryのインポート文追記

import chromedriver_binary
...
..
.
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