Web stamping automation in Chrome

Overview

It was troublesome to start up the computer every morning, open Chrome, and stamp it, so I automated it.

What you have prepared

・ Windows 8.1 ・ Google Chrome ・ Python 3 ・ Selenium ・ ChromeWebDriver

I'm sorry I don't remember the version, but all of them should have been the latest versions as of June 2019.

Preparing WebDriver

Download the Windows version of Chrome Driver for your Chrome version from here. For example, if you have a Chrome version of 78, you should have a Chrome Driver of 78. For other OS, please download the other OS version.

Place the downloaded Chrome Driver in a suitable place. (In the project folder, directly under C, etc.)

Implementation

Implement it in Python as follows.

clockin.py


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import time
import sys

#Element (constant)
ELEMENT_CLASS = "btnSTART"

#webdriver loading
driver = webdriver.Chrome(executable_path="Place where you left/chromedriver.exe")

#Start Chrome and jump to the specified URL
driver.get("https://www.clockin.hoge.jp")

#Wait until it is displayed
try:
    element = WebDriverWait(driver, 5).until(
        EC.presence_of_element_located((By.CLASS_NAME, ELEMENT_CLASS))
    )
finally:
    if not driver.find_element_by_class_name(ELEMENT_CLASS).is_enabled():
        driver.quit()

#Stamping
driver.find_element_by_class_name(ELEMENT_CLASS).click()

If the proxy cannot automate

If you have a proxy and you can't connect without logging in every time, it will stop when you start Chrome. It can be automated using an extension called SwitchyOmega. I will not write detailed usage, but if you access with Switchy Omega running and add proxy settings, a user folder will be created. Describe this as an option when loading the WebDriver.

Implementation

clockinproxy.py



#webdriver loading(chrome_options=option is an additional part)
driver = webdriver.Chrome(executable_path="./chromedriver.exe", chrome_options=option)

#Stop for a moment
time.sleep(1)

#Start Chrome and jump to the specified URL
driver.get("https://www.clockin.hoge.jp")

Due to proxy authentication processing, sometimes the screen does not jump to the specified URL and the screen becomes blank. Therefore, if you stop with sleep for about 1 second, it will jump to the URL without any problem.

reference

・ Https://qiita.com/memakura/items/20a02161fa7e18d8a693 I used it as a reference for manipulating the elements on the page.

Recommended Posts

Web stamping automation in Chrome
Web scraping notes in python3
Trade-offs in web scraping & crawling
UI Automation Part 2 in Python
python web scraping-get elements in bulk
Automatic minimum testing in web development
Web application development memo in python
Hit the web API in Python
web coder tried excel in Python