Automate your web logins
Script Limitations
The xdotool
and xte
utilities are great for simple web page automation when the HTML form items are sequential and no special decision making is required. Unfortunately, I found that when I tried to book a park time on the weekend, I started to see some limitations (Figure 4). During busy times, if I tried to book by time, xte
and xdotool
could not determine whether the time slot was taken. A simple workaround would be to search for the first Available or Not Busy slot, but this doesn't allow you to pick times you like.
For projects that require some logic (like choosing a good time from a list of times), Selenium with Python is an excellent fit.
Selenium with Python
Selenium [4] is a portable framework for testing web applications, with client-server tools and an IDE. The Selenium WebDriver component (available for Firefox, Google Chrome, Internet Explorer, Safari, Opera, and Edge) sends commands from client APIs directly to a browser. Client APIs are available for C#, Go, Java, JavaScript, PHP, Python, and Ruby. The Selenium Downloads page [5] has details on installation of the WebDriver scripts.
To install the Linux 32-bit Selenium driver (geckodriver
) for Firefox, enter:
wget https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-linux32.tar.gz tar -xvzf geckodriver-v0.24.0-linux32.tar.gz chmod +x geckodriver sudo mv geckodriver /usr/local/bin
To install the Selenium library for Python, enter:
pip install selenium
The big difference between the xte
or xdotool
utility and Selenium is that Selenium can access the HTML code of the selected web page directly.
Log In with Selenium and Python
As for xte
and xdotool
, you need to do some background manual work before writing the script. Once the required web page is open, you can use the Web Developer Inspector tool to examine HTML code. To access the Inspector, Select Tools | Web Developer | Inspector from the top menubar or use the shortcut Ctrl+Shift+C.
For the Netflix sign-in example, the Email or phone number and Password inputs are needed (Figure 5). When the Inspector is open, items selected on the web page are highlighted in the Inspector pane. In this example, the Email or phone number entry uses id="id_userLoginId"
, and the password entry uses id="id_password"
. Listing 3 shows the Python code that signs in to Netflix.
Listing 3
Netflix Sign-In with Selenium
01 # 02 # netflix_login.py - automate Netflix Login 03 # 04 from selenium import webdriver 05 06 url="https://www.netflix.com/ca/login" 07 email="my_email.com" 08 pwd="my_password" 09 10 browser = webdriver.Firefox() 11 12 browser.get(url) 13 14 # wait for page to refresh 15 browser.implicitly_wait(10) 16 17 username = browser.find_element_by_id('id_userLoginId') 18 username.send_keys(email) 19 20 password = browser.find_element_by_id('id_password') 21 password.send_keys(pwd) 22 23 password.submit() 24 25 print("Login Complete")
When a web page is called, it's important to give the page some time to refresh. The implicity_wait(10) call (line 15) waits up to 10 seconds for a Selenium query to complete.
HTML items can be found by either ID (find_element_by_id()
) or by name (find_element_by_name()
). A Selenium object needs to be created before initiating any action on it. Line 17 finds and then creates a username
object from ID 'id_userLoginId'
. The send_keys()
method is used to pass text strings to <input>
tags (lines 18 and 21). Calling the submit()
method on any input object will send all the form data as a request to the web server (line 23).
« Previous 1 2 3 Next »
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
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.