Facebook Login with Python

In this article, we will see how to log into a Facebook account using Python and the power of selenium.

from selenium import webdriver

from time import sleep


usr = input ( `Enter Email Id:`

pwd = input ( `Enter Password:`


driver = webdriver.Chrome ()

driver.get ( ` https://www.facebook.com/ ` )

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.

For security purposes, we can also enter a password without displaying it on the screen . To do this, we need to enable another module called getpass . Now, with one change in the password, we can enter the password without displaying it on the screen.

from getpass import getpass

pwd = getpass ( `Enter Password:`

Getpass asks the user for a password without repeating. It basically allows you to enter your password without showing it on the screen.

Similarly, you can also automate many other things like twitter, tweet, log out of facebook, and more.

In case of any requests, please post them below in the comments section. If you enjoyed this article and would like to see more stuff like this, let me know in the comments section below.

This article is provided by Umang Ahuja . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.

Get Solution for free from DataCamp guru