Automate Facebook Login Using Python

Hello there, my fellow Python programmers, today we are going to learn about how to automate Facebook login using Python programming.

For this task, we are going to use selenium and webdriver_manager libraries.

Let’s see what they do?



-> Selenium is a python library that is used because we want to automate and control the browser activity.

-> Many of the tasks in today’s world can be automated easily because of this library.

-> Automating tasks decreases human dependency so that they can work on something better.

-> We will be using it to get to the input fields as well as the login button on the Facebook web page.

-> We need to install this package in the python environment:

Install Selenium using “PIP”:

pip install selenium or pip3 install selenium.



-> Webdrive_manager is used because it will help us in opening a new page in the browser.

-> Here we wan to open the facebook’s login page so this function will be used.

-> We need to install this package in the python environment:

pip install webdriver_manager or pip3 install webdriver_manager

Now, let us see how we are going to code this:


Python code to automate Facebook login

Below is our code:

import time
from selenium import webdriver
from import ChromeDriverManager 
from webdriver_manager.firefox import GeckoDriverManager

# Initiliaze Webdriver
  driver = webdriver.Chrome(ChromeDriverManager().install())
  driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

def FacebookLogin():
  #Enter Your Email ID And Password
  user=input('Enter Email Id:')  
  password=input('Enter Password:')

  #Opening Facebook.
  print ("Facebook Opened") 
  #Entering Email and Password
  username_box = driver.find_element_by_id('email') 
  print ("Email Id entered") 
  password_box = driver.find_element_by_id('pass') 
  print ("Password entered") 

  #Pressing The Login Button  
  login_box = driver.find_element_by_id('loginbutton') 

  print ("Done") 
  input('Press anything to quit') 



Understanding the code:

-> We have imported the “time” library to use the sleep function.

-> This is done because while automating a process we need to keep in mind the fact that the site should not think of us as a bot.

-> So, the sleep function is used to create a delay of 1 sec before trying again.

-> Then we have initialized the webdriver function to an object “driver” for the respective browser in use.

-> find_element_id function is used to get the email, the password field tags, and the login button.

-> send_keys function is used to post the email or password in the respective field.

-> click () function is used to click on the login button.

-> driver.quit() function quits the process created by the driver object.

-> FacebookLogin() calls the function.


Note: We could have used the getpass() function instead of input() function to get the password so that it remains hidden when typed.

from getpass import getpass 
password = getpass('Enter Password:')



This code will automatically open and fill in all the details entered while running the script and click on the login button.

facebook login page


And that’s how you automate Facebook Login using Python.

I hope you loved the read.

Thanx for reading.


Leave a Reply

Your email address will not be published. Required fields are marked *