Selenium – not just a chemical element with atomic number 34

Do you have any mundane tasks that you have to do on a regular basis that don’t really require any thought or creativity, just that you get the task done? I definitely do.

One such task I am required to perform is to fill out a form each day and submit it, in the mornings, before work. First of all, I’m not sure that this is something that my workplace can legally demand of me, given that I am not clocked in to perform this task. But in any case, I am tasked with this annoyance. I am the kind of person who is hardly alive before having my first cup of coffee, and the form is due well before I’ve started my coffee a-brewin’ so I decided that this task is an excellent candidate for automation.

I set about looking for automation software – a macro recorder of some sort that I could use to record my actions of opening a browser, typing in the URL of the form, typing in my name to the form, clicking various buttons, and finally submitting it. The first software I tried would not record keyboard input. No good.

The next software wouldn’t record for long enough to capture all of the actions I needed included in the macro. Scratch that.

The next software was limited in features because it was the free version.

Argh…

As usual, I came to the conclusion that I would need to just write some code to handle the task. I often think there has to be some off the shelf solution for a given problem, only to find minutes, hours, or sometimes weeks later that I should have just written some code to start with.

So I opened up my terminal and installed the Python module that would be well suited for browser related task automatoin: Selenium.

And here is what I came up with:

import datetime
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

# Get today’s weekday number
today = datetime.datetime.now().weekday()

# If it’s the weekend, don’t run this task
if today < 6:

# Set up our browser object and open the health screening URL
browser = webdriver.Chrome()
browser.get(‘https://forms.gle/T3a2iHY2iJqgi8kbA’)

# Wait for the browser to finish loading
time.sleep(2)

# Locate the name input field, click it, and populate with name
element = browser.find_element_by_css_selector(‘INPUT.quantumWizTextinputPaperinputInput.exportInput’)
element.click()

Name changed to protect identity
element.send_keys(‘Channing Tatum’)

# Now for each of the questions, answer NO by hitting the radio buttons
element = browser.find_element_by_id(‘i12’)
element.click()

element = browser.find_element_by_id(‘i22’)
element.click()

element = browser.find_element_by_id(‘i32’)
element.click()

element = browser.find_element_by_id(‘i42’)
element.click()

# Wait just in case of…rain.
time.sleep(2)

# Finally, find the submit button and click that bad donkey.
button = browser.find_element_by_css_selector(‘DIV.appsMaterialWizButtonEl’)
button.click()

In the end, I decided against using it because ethical concerns (the answers might not be “No” every day to all the questions, and the form is meant to be answered honestly.)

Still, pretty slick. And a short, sweet example of what you can accomplish with just a few lines of code.