Image by mcmurryjulie from Pixabay
Contents
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 seleniumClick here if the PIP command is not working.
Download the following drivers according to the web browser you are going to use.
- Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
- Firefox: https://github.com/mozilla/geckodriver/releases
- Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/
- Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Extract and save the downloaded driver.
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 :
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 :
We will use xpath method provided by selenium to navigate the XML document and to find the required input fields.
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 :
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.