[PYTHON] Automatically manipulate web pages using selenium webdriver

Overview

I use software called Attendance Pro for attendance input, but I tried to automate it using selenium's web driver because daily input is troublesome. It's a very naive method, but I'm not very used to it, so as a first step. Also, make it an exe so that people other than yourself and people without a Python environment can use it.

(8/21 postscript) At first, I used Chrome's webdriver, but since the browser starts up every time, I changed to using PhantomJS as a headless browser. Download PhantomJS from here and specify the path of the folder containing phantomjs.exe in the environment variable Path.

Environment and preparation

The main software environment is as follows. OS: Windows 10 Python: 2.7.12 Selenium: 3.5.0 PyInstaller: 3.2.1 PhantomJS: 2.1.1

I didn't have selenium, pyinstaller, etc., so I installed it with pip.

pip install selenium
pip install pyinstaller
pip install ctype

code

Attendance and departure are in separate files (exe). I only go to work here, but it's almost the same.

attendancepro_start.py


import sys
import os
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
import time
from ctypes import *

#In order to distribute with exe, specify the user name and password each time
username = sys.argv[1]
password = sys.argv[2]

user32 = windll.user32

def page_is_loaded(driver):
    return driver.find_element_by_tag_name("body") != None

#Load the browser driver.
# driver = webdriver.Chrome("./chromedriver.exe")Originally implemented in Chrome, but changed to the following
driver = webdriver.PhantomJS(service_log_path=os.path.devnull)
#Load the target page. First, the login page.
driver.get("https://attendance.cvi.co.jp/LogOn.aspx")
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)

#After loading, find_element_by_Get the element by specifying it with xpath and give the input.
#In advance, display the source of the web page on the browser and search for the ID of the form you want to enter.
email_field = driver.find_element_by_xpath("//*[@id=\"txtUserID\"]")
email_field.send_keys(username)

company_field = driver.find_element_by_xpath("//*[@id=\"txtCompanyCode\"]")
company_field.send_keys("xxx")

password_field = driver.find_element_by_xpath("//*[@id=\"txtPassword\"]")
password_field.send_keys(password)

#After inputting all, screen transition
password_field.send_keys(Keys.RETURN)
wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)
time.sleep(5)

#Similarly, specify the ID of the place you want to enter and give the input.
#Here the state of the submit element(enabled/disabled)The cases are divided by.
#Also, the MessageBox makes it possible to know which branch it was.
if driver.find_element_by_xpath("//*[@id=\"ctl00_ContentMain_btnWebStartTime\"]").is_enabled():
    driver.find_element_by_xpath(
        "//*[@id=\"ctl00_ContentMain_btnWebStartTime\"]").click()  # Send day start
    user32.MessageBoxA(
        0,
        "Start done",
        "AttendancePro",
        0)
else:
    user32.MessageBoxA(
        0,
        "Start already done",
        "AttendancePro",
        0)

#When you're done, browser and shortcuts(See below)Close the window and exit.
driver.close()
os.system('taskkill /fi "WindowTitle eq attendancepro_start"')
sys.exit()

exe conversion

Convert to exe with the following command. ico is appropriate. pyinstaller --clean -F --icon=xxx.ico attendancepro_start.py

At this point, test the resulting exe below. ʻAttendancepro_start.exe ` When using Chrome driver, place it in the same folder as exe.

Actions required after distribution

Since it is troublesome to execute it at the command prompt one by one, it is assumed that the exe is executed as a shortcut. Arguments are specified in the shortcut properties. image

With this method, the command prompt window remains at the end, so taskkill is used to close the window at the end. I don't think I usually do this, but I didn't know how to do it easily, so I'm doing it ...

Miscellaneous feelings

It's been a lot easier. Also, I learned that I can operate web pages with python, so I hope it can be applied to something. Python3.x is almost okay, but only the first character is displayed in MessageBoxA used in the middle. MessageBoxW is fine, but in the future it will be garbled in 2.x. Since it is supposed to be used in exe anyway, I will close my eyes this time.

reference

How to specify elements of a web page in Selenium

How to specify variables later in the exe version

Message Box Parameters (https://msdn.microsoft.com/en-us/library/cc410914.aspx)

Close the window after running the windows shortcut (https://stackoverflow.com/questions/16599655/how-to-close-a-cmd-window-using-python-2-7)

[Only the first character is displayed in MessageBox](https://stackoverflow.com/questions/38302830/python-message-box-shows-only-the-first-letter-of-my-title-and-my- message)

Recommended Posts

Automatically manipulate web pages using selenium webdriver
Try tweeting automatically using Selenium.
Web scraping using Selenium (Python)
[Python] Introduction to scraping | Program to open web pages (selenium webdriver)
Try using Selenium
Web application using Bottle (1)
Python web scraping selenium
Operate your browser using the Selenium Web Driver Python bindings