[PYTHON] Taro who pushes the attendance button instead of you

Introduction

This is my first time to participate in the Advent Calendar. I'm sorry, but I hope it helps someone: nerd:

As for the main subject, what is your company's attendance system? We use web attendance (e-clocking). Go to work, start your PC, launch your browser, access e-clocking, log in, and go to work! : runner: It takes a lot of time and effort. Every morning I thought this was a pain and tried to automate it using Python and Selenium libraries. If you remember this, you can automate simple browser-based operations, so you can use it for E2E testing and other small tools.

Premise

It is assumed that you have an environment that can run Python 3 and a Chrome driver.

Implementation

Suddenly source

syukkin.py


#Attendance push Taro
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import configparser
import os
import datetime

inifile = configparser.ConfigParser()
path = os.getcwd()
#Reading the login configuration file
inifile.read(path+'\\kintai.ini', 'UTF-8')

#Start Chrome
options = webdriver.ChromeOptions()
options.add_argument('--headless')                  #Headless mode
options.add_argument('--disable-gpu')               #Arguments that seemed to be needed in the past
options.add_argument('--ignore-certificate-errors') #Ignore SSL error
#Specify the version of chromedriver
driver = webdriver.Chrome(options=options, executable_path='Where to put the chrome driver\\chromedriver.exe')

#Access to attendance
driver.get('https://cl.i-abs.co.jp/e-clocking/login.asp')
print(driver.title)
#Get frame and switch
frame=driver.find_elements_by_xpath("//frame")[1]
driver.switch_to.frame(frame)
time.sleep(1)

driver.find_element_by_name('DataSource').send_keys(inifile.get('user_info', 'company'))
driver.find_element_by_name('LoginID').send_keys(inifile.get('user_info', 'id'))
driver.find_element_by_name('PassWord').send_keys(inifile.get('user_info', 'pass'))
driver.find_element_by_name('btnLogin').click()

time.sleep(1)
driver.find_element_by_name('IN').click()
driver.find_element_by_name('punch').click()
time.sleep(1)
driver.save_screenshot(str(datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")) + '.png')
driver.find_element_by_name('OK').click()
driver.quit()

kintai.ini


[user_info]
company=Value to enter in DataSource
id=User ID
pass=password

Commentary

I will explain the option settings before starting the driver and the automatic operation part separately.

Option setting

#Start Chrome
options = webdriver.ChromeOptions()
options.add_argument('--headless')                  #Headless mode
options.add_argument('--disable-gpu')               #Arguments that seemed to be needed in the past
options.add_argument('--ignore-certificate-errors') #Ignore SSL error

A screen is required for human operation, but the program can operate without problems if the corresponding tag can be identified from HTML, so pass the option of headless mode without screen. Lighter movement: muscle:

Chrome Driver version specification

#Specify the version of chromedriver
driver = webdriver.Chrome(options=options, executable_path='chromedriver.exe')

I purposely specify the location of chromedriver.exe in webdriver. This is because the Chrome browser update makes it out of the version supported by Selenium, and one day the program suddenly stops working. Even though the economy was running well until yesterday! !! I panicked when I didn't know: fearful: You can refer to this article for this error Errors that occur between Chrome Driver and Chrome version

** Addendum 2020/01/30 ** Other than the unsupported version of the library, it seems to occur even if the version of Chrome and Chrome driver you are using are different. Check the version of Chrome once and get the same version again. ChromeDriver

Automatic operation

#Access to attendance
driver.get('https://cl.i-abs.co.jp/e-clocking/login.asp')
print(driver.title)
#Get frame and switch
frame=driver.find_elements_by_xpath("//frame")[1]
driver.switch_to.frame(frame)
time.sleep(1)

Access the e-clocking URL. I have a habit here immediately. Since the Frame tag is used, Taro starts to operate on the file that has no attendance button at all. So I will instruct you to work with the second person reading.

driver.find_element_by_name('DataSource').send_keys(inifile.get('user_info', 'company'))
driver.find_element_by_name('LoginID').send_keys(inifile.get('user_info', 'id'))
driver.find_element_by_name('PassWord').send_keys(inifile.get('user_info', 'pass'))
driver.find_element_by_name('btnLogin').click()

time.sleep(1)
driver.find_element_by_name('IN').click()
driver.find_element_by_name('punch').click()
time.sleep(1)
driver.save_screenshot(str(datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")) + '.png')
driver.find_element_by_name('OK').click()
driver.quit()

All you have to do is find and operate the input box and button with find_element_by_name, relying on the name attribute. Get the input value from the inifile and enter it. Click the login button to log in. After that, press the attendance button and then the confirmation button. Since the screen is not displayed at all, take a capture of the stamped screen with save_screenshot just in case.

Actually, it is better to put in a process to wait until the screen is read, but put sleep (1) to make the tea muddy. (If it takes a long time to load the screen, it may fall around here)

By the way, if you want to leave work, go to element_by_name ('IN') driver.find_element_by_name('OUT').click() Just do.

Batching

Since it is troublesome, I want to finish it by just double-clicking the batch file, so I will do this.

syukkin.bat


The folder where the cd py files are located
python syukkin.py

You can use it immediately by putting a bat file on your desktop. It might be better to make it an exe file.

in conclusion

It's very simple, but I stumbled upon it when I made it (especially the Chrome driver version ...), so I hope it helps. If you have any additional information or improvements, please make an edit request: bow_tone1:

Recommended Posts

Taro who pushes the attendance button instead of you
Check the type of the variable you are using