Automate your web logins

Log In and Go

© Lead Image © thotti, photocase.com

© Lead Image © thotti, photocase.com

Article from Issue 249/2021
Author(s):

Automated web logins with command-line tools and Selenium ensure you don't miss scheduling an activity.

During the COVID-19 lockdown, many activities like pools, gyms, and golf courses required people to sign in to websites before they could access these activities. These precautions helped to maintain a safe environment; however, the booking process was awkward, and it was easy to miss an activity if you weren't signed up early enough.

Luckily, some great Linux tools can automate web logins. In this article, I share two techniques to create automated web logins. The first technique uses command-line tools like xte and xdotool. This approach allows simple Bash scripts to replicate how you would use keystrokes to access web pages.

The second technique uses the Selenium Python library. The Selenium API allows you to tackle more complex projects by giving you access to the full Document Object Model (DOM) of a web page.

Keyboard Simulation

The most popular choices for keyboard and mouse simulation are the xautomation package [1] and the xdotool utility [2]. The xdotool utility is feature-rich, with special functions for desktop and window functions. The xte tool, a part of xautomation, is a little simpler, focusing entirely on keyboard and mouse simulation.

The wmctrl [3] utility is also very useful to help you determine which windows are open on your desktop, and it can also set the active window with a text substring.

In Ubuntu, enter

sudo apt-get install xautomation xdotool wmctrl

to install the xautomation package and the xdotool and wmctrl utilities.

Log In with xte

With the xte utility, you can send a single keyboard character or strings of characters. A Bash script that uses xte commands can emulate your actions to log in manually to a web page.

Typically people use the mouse on web pages, which is quite different from logging in 100 percent with the keyboard. Web pages often have a number of clickable items before the main form entry area, so it is important to step through and document the login procedure manually. A good simple example is to try and log in to Netflix (Figure 1).

Figure 1: Netflix sign in with the xte command.

The Bash script in Listing 1 uses xte to automate the Netflix sign-in. This script opens a Chrome browser page (line 10) and then sets the focus to this page (line 12). Next, it sends the correct tab, text, and return key sequences (lines 15-22).

Listing 1

Netflix Sign-In

01 #!/bin/bash
02 # netflix_login.sh - script logs into Netflix
03 #
04
05 url="https://www.netflix.com/ca/login"
06 email="my_email.com"
07 pwd="my_password"
08
09 # open browser to wait for the page to open, then set focus to it
10 chromium-browser $url &
11 sleep 2
12 wmctrl -a "Netflix - Chromium"
13
14 sleep 1 # allow time to get focus before sending keys
15 xte "key Tab"
16 xte "key Tab"
17 xte "str $email"
18 xte "key Tab"
19 xte "str $pwd"
20 xte "key Tab"
21 xte "key Tab"
22 xte "key Return"
23
24 echo "Netflix Login Done..."

Setting the window focus can be tricky if you have a number of windows open. The command wmctrl -l lists all open windows, and the command

wmctrl -a '<some title info>'

sets the mouse and keyboard focus to a specific window from a substring of the window title.

Book with xdotool

The xdotool syntax also sends keystrokes and text and is very similar to xte, but with a few extra features. A park booking example (Figure 2) is a bit more complex, because a booking time needs to be selected from a list. For this project, the automation script needs to manage eight entry fields (to keep things simple, I'll pass the date in the URL) and select a time slot.

Figure 2: Automating a park booking with a time search.

Neither the xte nor xdotool utility supports a search text function. A simple workaround is to use the web browser's search function. By enabling caret (text cursor) navigation, it's possible to move the active cursor location according to the browser's search results.

The caret dialog is shown by pressing F7 (Figure 3). It's important to note that the caret enable/cancel and Yes/No buttons can vary between browsers.

Figure 3: Improve keyboard navigation with caret browsing.

The Bash script in Listing 2 uses the browser's search dialog to find and select a 10:00am time slot for a park. One of the first steps is to enable caret navigation (lines 12-13).

Listing 2

Book a Park Visit

01 #!/bin/bash
02 # book10am.sh - make a 10:00 park booking
03 #
04 sdate="startDate=2021-04-23" #adjust the date
05 url="https://book.parkpassproject.com/book?inventoryGroup=1554186518&&inventory=1229284276&$sdate"
06
07 chromium-browser $url & #open browser to park booking page
08 sleep 5 # wait for browser to come up
09 wmctrl -a "Chromium"
10 sleep 2
11 # Turn on caret browsing
12 xdotool key F7
13 xdotool key Return
14 sleep 1
15
16 # tab to 'Time Slot' area
17 tabcnt=8
18 xdotool key --repeat $tabcnt --delay 100 Tab
19
20 xdotool key Return
21 sleep 1
22
23 # Search for 10:00 time and select it
24 xdotool key ctrl+f
25 xdotool type '10:00'
26 xdotool key Return
27 # Close find dialog and select time
28 xdotool key Tab Tab Tab Return Return
29
30 echo "Park Time Booking Complete"

A useful feature of xdotool is the repeat with a delay option (lines 17-18). In this script, I used this feature to tab eight times to get to the Time Slot field. A Ctrl+F keystroke opens the browser search dialog (line 24). Next, the xdotool type option passes in the '10:00' time string (line 25). The final step is to close the search dialog and hit Return to select the 10:00 AM – 12:00 PM time slot (line 28).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Web Regression Test

    Testing complex web applications does not necessarily mean investing in expensive, proprietary tools such as Test Director or Silk Performer. Selenium is for free; it can remotely control any major browser, and it is programmable in Perl.

  • Workspace: Text Expander

    A couple of utilities and a dash of Bash scripting are all you need to roll out a simple yet flexible text expander.

  • Tutorial – Devilspie2

    Stop battling your window manager to position things as you like – make scripts do all the hard work!

  • Kitchen Kiosk

    Create a kiosk display from an old eReader to show data culled from Home Automation, Raspberry Pis, and Arduinos.

  • Servile Guardian

    What is making the lights on the router flicker so excitedly? An intruder? We investigate with pfSense on a Protectli micro appliance and a screen scraper to email the information.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News