The Wayback Machine - https://web.archive.org/web/20240624100250/https://www.geeksforgeeks.org/facebook-login-using-python/
Open In App

Facebook Login using Python

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them. In this particular article, we will see how to log in to the Facebook account using Python and the power of selenium.

Selenium

automates and controls browsers and it’s activity. We can code in our way to control browser tasks with the help of selenium. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can be automated as well. As you learn more it’s so much fun to see things happening automatically and saving time in doing useless tasks again and again. We use selenium here to open the site of our requirement (in this case Facebook) and there we inspect elements across email box, password box, and login button to find the id of them.

  • Using find_element(By.ID) function provided by selenium module, we can find the required element (username box, password box, login button)
  • Using send_keys() function, provided by selenium module, we will send the data into the box.
  1. Installing third party modules required
    Selenium 
    getpass
    Additional Requirement : geckodriver for firefox and 
                             chromedriver for chrome
    
    
  2. Importing necessary modules
    • Selenium : to automate browser
    • Time : to pause running of script for some seconds as browsers try to detect automation stuff if we input too fast
  3. Taking username and password as input from user Using input() function and passing prompt message as argument.
  4. Opening browser and required websitewebdriver.Chrome() will open new window of chrome. We will save it’s object in variable named driver. Now using get function we will open up the Facebook website.
  5. Finding element for sending data and Sending input Use inspect element tool on the element of browser of which you want to find id. In this case we will inspect username box, password box, login button to find their id. And then use this id combining with selenium function find_element(By.ID) to find it across web page and save it in variables for later use. Then by using send_keys() we will send data across the elements found previously.
  6. Closing the browser After all of the above steps we have to quit the session and will be achieved by using driver.quit(). Note: Here driver is the name of variable you chose for webdriver.Chrome().

Complete Code:

Python




from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
 
usr=input('Enter Email Id:')
pwd=input('Enter Password:')
 
driver = webdriver.Chrome(ChromeDriverManager().install())
print ("Opened facebook")
sleep(1)
 
username_box = driver.find_element(By.ID, 'email')
username_box.send_keys(usr)
print ("Email Id entered")
sleep(1)
 
password_box = driver.find_element(By.ID, 'pass')
password_box.send_keys(pwd)
print ("Password entered")
 
login_box = driver.find_element(By.ID, 'loginbutton')
login_box.click()
 
print ("Done")
input('Press anything to quit')
driver.quit()
print("Finished")


See how such a concise piece of code can automate things for you.

Bonus:

We can also

enter the password without displaying it on screen

, for security purpose. For that we have to include one more module called

getpass

. Now with just one change in input statement of the password we can input password without displaying it on screen.

Python




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


Getpass prompts the user for a password without echoing. Basically it lets you enter the password without showing it on the screen. Similarly you can also automate many other things like twitter login, tweeting, Facebook logout, and much more.

In case of any queries, post them below in the comments section. If you liked this article and want to see any more of the similar stuff, let me know in the comments section below.

This article is contributed by

Umang Ahuja

. If you like GeeksforGeeks and would like to contribute, you can also write an article using

write.geeksforgeeks.org

or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.



Previous Article
Next Article

Similar Reads

Create MySQL Database Login Page in Python using Tkinter
Prerequisites: Python GUI – tkinter, Python MySQL – Select QueryTkinter is one of the Python libraries which contains many functions for the development of graphic user interface pages and windows. Login pages are important for the development of any kind of mobile or web application. This page is most essential for user authentication purposes.We
2 min read
Login Application and Validating info using Kivy GUI and Pandas in Python
Prerequisites : Kivy, Pandas Kivy is a multiplatform GUI library, known for being responsive. It provides management of multiple screens in a single application. In this application we will be using multiple screens to log in user's info and validate it. We will save the information in a csv file and use pandas to validate the information inside of
5 min read
Login Twitter using Python Selenium
Project Description:- Here, we're going to study a simple how-to log in twitter by selenium. Selenium is a free tool for automated trying out across exceptional browsers. Requirement: Selenium – Selenium Python Introduction and Installation Below are the steps: First, go to the twitter website using this Link.Then click on investigate element by ur
2 min read
Create a Modern login UI using CustomTkinter module in Python
In this article, we will see how we can create a simple modern login UI using the customtkinter module in Python. customtkinter is a module that is purely built upon the Tkinter module, which helps the developers to create modern UI which was not possible with the traditional Tkinter module. It supports various new, modern, and fully customizable w
5 min read
Scrape LinkedIn Profiles without login using Python
In this article, we'll explore how to scrape LinkedIn profiles without the need for a login, empowering you to gather valuable insights and information programmatically. By leveraging Python's web scraping capabilities, we can access public LinkedIn profiles seamlessly, opening up new possibilities for data-driven analysis and connection building."
3 min read
Gmail Login using Python Selenium
Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them. In this particular article, we will see how to log in to the Gmail account using Python and the power of selenium. Selenium automates and controls browsers and it’s activity. We can code in ou
4 min read
How to access popup login window in selenium using Python
Many websites use sign-in using social media to make the login process easy for users. In most cases, if the button is clicked then a new popup window is opened where the user has to enter their user credentials. Manually one can switch windows in a browser and enter the required credentials to log in. But in case of unattended web access using a w
3 min read
Download Facebook video using Python
In this article, we will learn how to create a user-friendly Python program to download Facebook videos using the video URL. Using Python programming, there are several ways to solve the same problem. we going to use a Python module called 'youtube_dl' to achieve the target. Description of modules and methods used Before diving into the solution, l
4 min read
Login and Registration Project Using Flask and MySQL
Project Title: Login and registration Project using Flask framework and MySQL Workbench. Type of Application (Category): Web application. Introduction: A framework is a code library that makes a developer's life easier when building web applications by providing reusable code for common operations. There are a number of frameworks for Python, inclu
6 min read
Login Authentication using Express.js, Passport.js and BCrypt
In this article, we will create a Login Authentication application. This application basically displays a login/register form interface and authenticate the user. All this logic of login/register authentication is implemented using express, mongodb, passport and bcrypt and frontend is created using ejs. Preview of Final Output: Prerequisites and Te
9 min read
Practice Tags :
three90RightbarBannerImg