Sunday, January 3, 2021

Python bot to automate Facebook Login

Image by mcmurryjulie from Pixabay
 

Contents

  1. Introduction
  2. Resources
  3. Source Code
  4. Explanation

Introduction: Python bot for automatic Facebook login 

Through programming, we can efficiently automate lots of day-to-day works and it can be achieved through bots.
By creating bots we can save ourselves from borings, repetitive, and time-consuming tasks. So today we are going to build a python bot that will automate the Facebook login process. Further, we can extend the functionalities of this bot to automate the chat response as well in the upcoming articles.


For this project, we are going to use the Selenium module.
The entire selenium framework was created for testing websites and web applications. 
You can download the selenium module into the local directory of your project or you can run the pip command in the terminal or in cmd.

pip install selenium 
Click here if the PIP command is not working.

Download the following drivers according to the web browser you are going to use.


from selenium import webdriver
#section1
# Setting up the Chrome driver and opening the facebook LoginPage
driver = webdriver.Chrome(executable_path=r'C:\Users\Username\Documents\python\chromedriver_win32\chromedriver.exe')
driver.get("https://www.facebook.com/login.php?login_attempt=1&lwv=110")

#section2
# finding the email_input box and entering the Email
email = driver.find_element_by_xpath("//input[@id='email' or @name='email']")
email.send_keys('Your Email ID')

# finding the password_input box and entering the Password
password = driver.find_element_by_xpath("//input[@id='pass']")
password.send_keys('Your passeword')

#section3
# Clicking the login button
button = driver.find_element_by_xpath("//button[@id='loginbutton']")
button.click()

Explanation:

Section 1 :

Our first task is to run the web-browser with the help of the drivers we have downloaded, In this article, we are going to use Chrome. 
Pass the driver path in the method 'webdriver.Chrome()'. It will initiate the required resources and open Chrome. This method returns an object of type "selenium.webdriver.chrome.webdriver.WebDriver", which we have stored in a variable name driver.

Next, we will navigate to the Facebook login page with the help of the method "driver.get()". This method accepts the page URL as an argument and opens the respective webpage in the browser.

Section 2  :

Once the login page is loaded our next task is to search for the email and the password input fields from the page HTML.
We will use xpath method provided by selenium to navigate the XML document and to find the required input fields.

Screenshot of facebook login page
XPath for email input box

Screenshot of facebook login page
XPath for password input box

The method driver.find_element_by_xpath() takes the xpath string as an argument and returns the respective object, this object can be used to access the particular fields of the loaded webpage.
Now our next step is to send the data (email ID and the password) to these fields through the respective objects. And for this, we can use the send_keys() method and pass the email and the passwords.

Section 3  :

Till here we are done with most of the code, now we are just left with the action of clicking the login button. To perform this clicking action, first, we have to find the login button with the help of its XPath string as we did before in section 2 and store the instance in the variable named 'button'. 
As a final step call button.click() to programmatically click the login button on the web-page.

In upcoming python projects, we will extend the functionality of this code to create a complete Facebook bot, which will allow the user to automate the login process as well as the chat response. For now, you can use this code and modify it as you want, to create new tools and programs.