Automate your web logins
Log In and Go
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).
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.
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.
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
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.