Automate Web Page Logins

To ensure a safe number of users during Covid we had to log into Web sites for access to activities like pools, gyms and ski hills. These precautions made sense, however the booking process was awkward and was easy to miss an activity if you weren’t signed up early enough.

Luckily there are some great Linux tools that can be used to automate web logins. If you are working in MS-Windows take a look at SendKeys command.

Linux Mouse and Key Stroke Simulation

In the Linux environment there a number of choices, such as xautomation and xdotools.

I thought that the xdotools package was feature rich with a number of special functions for desktop and windows functions. The xautomation package was a little simpler and it has some Python wrappers.

I found that it was very useful to install wmctrl, this allows you to easily see which windows are running and it can set the active window with a substring (you don’t need the full window name).

To install xautomation and wmctrl in Ubuntu:

sudo apt-get install xautomation wmctrl

Log in Example

To create an automation script you need to do the required steps once manually and document what is a Tab, entry field or an Enter/Return key. Some trial and error work will probably be required for timing if you are working between pages links.

A good simple example is to try and log into Netflix.

The following Bash script will : 1) open a Chrome browser page, 2) set the focus to the page and 3) sent the correct tab, text and a return key.


#!/bin/bash
# netflix_login.sh - script logs into Netflix
#

url="https://www.netflix.com/ca/login"
email="your_email.com"
pwd="your_password"

chromium-browser $url & #open browser to

# wait for the page to open, the set focus to it
sleep 2
wmctrl -a "Netflix - Chromium"
sleep 1 # allow time to get focus before sending keys
xte "key Tab"
xte "key Tab"
xte "str $email"
xte "key Tab"
xte "str $pwd"
xte "key Tab"
xte "key Tab"
xte "key Return"

echo "Netflix Login Done..."

Use wmctrl -l to see which windows are open.

A specific window can have the focus based on a substring, with the -a options

The xte command uses a string to pass key, text and mouse actions to the active window.

Final Comments

Using simulated keyboard and mouse movements is a nice tool for automating simple web pages.

If you need to look at more complex web automation solutions check out Selenium, it has a Python library that allow you to access the DOM object of the open web browser page.