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
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.