Automate your Facebook logins with Selenium and Python (Linux)

Amritya Singh
Floppy Disk F
Published in
3 min readMay 12, 2020

I’ve always been a lazy saint. I find ways to dodge work in every possible way. I can even fake my own death if it means skipping work. But sometimes when push comes to shove, and there’s no escape, I try to find ways to simplify my work, even if it’s a simple task of logging into my Facebook account (or any platform for that matter). Since the whole operation (launching the browser, opening the website, typing your login details and finally logging in) can be cut down to a single-short command execution, I’d like to share it with all the lethargic people out there. So let’s get started.

Installing Selenium

We’ll start by installing Selenium .The Selenium package will help us automate web browser interactions with Python. It is a super super helpful package. Just run the following command to install it.

pip3 install selenium

Installing Web Browser Driver

Now Selenium’s supported WebDriver implementations are restricted to Firefox, Chrome, IE and Remote. I’ll narrow this article’s scope to Chrome and Firefox, since they are unarguable the two most popular web browsers. You’ll need to have at least one of them installed. Next we will install their respective drivers.

ChromeDriver (for Chrome) download https://chromedriver.chromium.org/downloads

Geckodriver (for Firefox) downloadhttps://github.com/mozilla/geckodriver/releases

After downloading. Go to the Download destination and extract the downloaded file. Open the extracted folder. Press Right click and select Open in Terminal. Then run the following command.

chmod +x geckodriver 

Replace geckodriver with chromedriver if you want to install it for Chrome.

The Coding Part

Now fire up your Code Editor (I usually use VS Code and Sublime for Python) and copy the code. We’ll be storing our Facebook email id and password in the script and the it’ll do the rest of the work. I’ve tried my best explaining most of the things inside the script. But I’ll recommend you to go through this article for better clarity — https://www.javatpoint.com/selenium-python.

The Python Script

Now save your python script (preferably in a safe location) and needless to say don’t share it with anyone.

Let’s say you save your script as fbauto.py and save it in the Downloads directory. Run the script to see if it works.

cd /home/usr/Downloadspython3 fbauto.py

If you’re using VS Code you can run it directly from within the Code Editor.

Turning it into a Custom Terminal Command

This is where it gets interesting. Now we’ll be turning our script into a terminal command. Why? To reduce the steps involved. Now, run the following command:

nano .bash_aliases

This will open the nano text editor. As the name suggests, bash_aliases contains the list of terminal command aliases, you’ll most probably find it empty. Now, we will define our custom terminal command.

alias fblogin='cd /home/bananaboi/Downloads; python3 fbauto.py;cd ~'

Here, fblogin is my alias (the name of my custom command), you can change it to whatever you like. Remember we have saved our script (fbauto.py) in the Downloads directory. So, in order to carry out the whole operation, we’ll first need to get to the destination of the script, execute it and then go back to the home directory. That will require 3 different commands that we have clubbed together, separated using — ; (semicolon) and contained within ‘ ‘ (single inverted commas) in a single line.

Be careful not to leave spaces on either side of the equal to sign (=).

Now save the file by pressing Ctrl+S, and then exit nano editor using with Ctrl+X. And run the following command in the terminal:

~/.bashrc

Check if the alias has been updated with:

alias

If you can find your alias name listed, then it has been successfully updated.

Now, run your custom terminal command:

fblogin

And see the magic happen before your eyes.

We have condensed the lengthy process (maybe not so lengthy) of logging into our Facebook accounts to a simple task of running a small terminal command. The amalgamation of Selenium and Python can accomplish amazing feats when used handily. Be sure to try your hands at it. When you couple procrastination with the benevolent technological blessings of the 21st century, then the possibilities are infinite!

--

--