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
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.