Python Tutorial | How to log into facebook with Selenium

Reading Time: 3 minutes

Python Tutorial | Logging into facebook using Selenium

If you’re lazy like me, I would do anything I can to avoid doing repetitive tasks over and over. I would rather spend 20 hours learning how to do a task once with a script, than spending 3 hours doing the same process over again. While researching how to auto processes within an internet browser, I stumbled upon Selenium. 

Pulled from the Selenium site itself “Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well.”

I figured my first project using the selenium library should be a facebook automation login script. Usually when I’m not busy at work, I open up an incognito window, log into facebook, and check out whats going on with the world. I figured I can save myself the time by creating a small script I can double click on and have it input the information for me. And that is how I came up with the python code below.


The Code

Download the script on github


How the script works

First we import in the webdriver, which controls the browser of our choice(I used Chrome) and then we imported Keys. Keys sends an input to the browser.

We give the Webdriver a variable name called driver and on the next line we tell the web browser to open

Then I created a function called userInfo, which passes two arguments, username and password. The function simply finds the username and password input box on facebooks home page, passes the arguments over and clicks on the login button.

Just like that, In 15 lines code, something that took me 15 seconds to complete multiple times a day, now takes 2-3 seconds. I love automation.

Feel free to leave a comment if you run into any troubles with the script or have any other ideas on what we can create with selenium. Our imaginations are limitless.


Interested In Learning Python Automation?

Check out “Automate the Boring Stuff with Python: Practical Programming for Total Beginners“.

In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:

–Search for text in a file or across multiple files
–Create, update, move, and rename files and folders
–Search the Web and download online content
–Update and format data in Excel spreadsheets of any size
–Split, merge, watermark, and encrypt PDFs
–Send reminder emails and text notifications
–Fill out online forms

Click the image below to purchase!



This post is located under the Programming Category

Enjoyed this Python Tutorial? Check out Website Status Monitor Written in Python



Leave a Reply

Your email address will not be published.