Facebook Login using Python

PythonServer Side ProgrammingProgramming

We can use the python package called selenium to automate the interaction with webdrivers. In this article we will see the interaction between python’s selenium package and logging in to Facebook.

Approach

Selenium package is used to automate and controls web browsers activity. Out python code will need the selenium package to be installed and also a driver software known as geckodriver to be available for the program. Below are the steps to achieve this.

Step-1

Install selenium in you python environment

pip install selenium

Step-2

Download the geckodriver from this link. Place it in the same directory where we are going to have this python script.

Next we create the program which will import the relevant modules form the selenium package and able to open the webpage for login.

To get the id details of the input boxes for login, we can view the source code of the webpage facebook.com and find the id of the fields as follows.

The code below contains the comments that explain the purpose of the code segments.

Example

from selenium import webdriver
#Open Firefox
browser = webdriver.Firefox()
# Go to the Facebook URL
browser.get("http://www.facebook.com")
# Enter the username and Password
uname = browser.find_element_by_id("email")
psword = browser.find_element_by_id("pass")
submit = browser.find_element_by_id("loginbutton")
# Send the details to respective fields
uname.send_keys("hello@gmail.com")
psword.send_keys("thepassword")
# Automate Click Login
submit.click()

Running the above code gives us the following result −

With the correct credentials the facebook login will happen automatically and you can see the logged in page. Selenium’s features enable this functionality.

raja
Published on 20-Dec-2019 11:31:19
Advertisements