Facebook Login using Python

Python scripting is one of the most intriguing and fascinating thing to do meanwhile learning Python. Automation and controlling browser is one of them.

In this particular article, we will see how to log into facebook account using Python and 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 element across email box, password box and login button to find 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 website
    webdriver.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 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
usr=input('Enter Email Id:'
pwd=input('Enter Password:'
driver = webdriver.Chrome()
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 getpass. Now with just one change in input statement of 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 let’s 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 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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter