Open In App
Related Articles

Facebook Login using Python

Improve Article
Save Article
Like Article

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.


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
    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:


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

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


We can also

enter the password without displaying it on screen

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


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


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

or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Last Updated : 22 Sep, 2023
Like Article
Save Article
Similar Reads
Related Tutorials