Facebook Login using Python

We can use the python package called selenium to automate the interaction with webdrivers. In this article we will see the interaction between python’s selenium package and logging in to Facebook.


Selenium package is used to automate and controls web browsers activity. Out python code will need the selenium package to be installed and also a driver software known as geckodriver to be available for the program. Below are the steps to achieve this.


Install selenium in you python environment

pip install selenium


Download the geckodriver from this link. Place it in the same directory where we are going to have this python script.

Next we create the program which will import the relevant modules form the selenium package and able to open the webpage for login.

To get the id details of the input boxes for login, we can view the source code of the webpage facebook.com and find the id of the fields as follows.

The code below contains the comments that explain the purpose of the code segments.


from selenium import webdriver
#Open Firefox
browser = webdriver.Firefox()
# Go to the Facebook URL
# Enter the username and Password
uname = browser.find_element_by_id("email")
psword = browser.find_element_by_id("pass")
submit = browser.find_element_by_id("loginbutton")
# Send the details to respective fields
# Automate Click Login

Running the above code gives us the following result −

With the correct credentials the facebook login will happen automatically and you can see the logged in page. Selenium’s features enable this functionality.

Updated on: 20-Dec-2019


Kickstart Your Career

Get certified by completing the course

Get Started