How to make a Facebook bot?
Now Facebook Bot is a very popular word, everyone is now familiar with bots either directly or indirectly?
So basically, what is an Internet Bot?
Bots are basically the internet robots that make things automated using scripts. You might have seen the robots in car manufacture companies.
Step by step: How to create a Facebook Bot:
- Python Interpreter + Selenium
- Browser + Driver
- Some basic knowledge of programming.
Step 1: Installing Python on PC
If you are using a Windows PC, you can download the Python Interpreter from their official site. In case you are using a Linux based PC, you can install it through command prompt by “$ sudo apt-get install idle” command.
Step 2: Installing Selenium on PC
Selenium is a web browser automation tool used for testing purpose.
Command for installing: Write “pip install selenium” on the command prompt.
Step 3: Installing web browsers driver for running the Script
You can download it via below links:
Step 4: Coding for Facebook Bot
Make sure Web-driver and code are saved in same the same folder.
Now coming to the main part, here we will coding staff with Python and Selenium.
-
Importing the necessary item for Script.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.common.exceptions import NoSuchElementException
import time
It will import everything that is needed for running the script.
-
Initiating the web browsers and opening Facebook URL(Uniform Resource Locator).
driver = webdriver.Chrome("C:\\Users\\User\\Desktop\\chromedriver.exe")
driver.get('https://www.facebook.com/login.php?login_attempt=1&lwv=110')
print("Opened facebook...")
Above code will open the Chrome browsers with the help of driver that we recently installed on PC. The second line of above code will lend us to Facebook login page.
-
Entering login ID and password for the Facebook Bot.
email = driver.find_element_by_xpath("//input[@id='email' or @name='email']")
email.send_keys('LoginID')
print("Email Id entered...")
password = driver.find_element_by_xpath("//input[@id='pass']")
password.send_keys('Password')
print("Password entered...")
Don’t get confused with syntax, driver.find_element_by_xpath
this is one of the predefined syntaxes by selenium and "//input[@id='email' or @name='email']"
this is something you can see on your screen by ctrl+shift+i on the keyboard.
-
Login to Facebook using Selenium facebook bot.
button = driver.find_element_by_xpath("//button[@id='loginbutton']")
button.click()
print("FB opened")
This will open your Facebook profile.
-
The final step, Posting on the wall by Script using Facebook Bot.(
statusbox.send_keys("Bot is typing here");
print("Status trying")
postbutton = driver.find_element_by_xpath("//button[contains(.,'Post')]")
postbutton.click()
print("post done")
The script is done.
Whole code- Github.com/Satyampd
Once you are done with coding, try to run code using Python IDLE.
If you want to learn more about Selenium: http://www.seleniumhq.org/docs/
If you want to learn more about Python: https://docs.python.org/3.5/tutorial/
- Best online course for automation: Automate the Boring Stuff with Python Programming
- Best online course for Python: The Python Bible | Everything You Need to Program in Python
- Best online course for Selenium: Selenium WebDriver With Java – Novice To Ninja + Interview
Any doubt or suggestion make sure to comment below.
[…] Though many us do not know about it, we had used it many times for our assignment and works. Quora is a platform where people ask questions and get the answer from some other fellow Quora users. When people like your answer they upvote it, similar Like button on Facebook. […]
[…] should know that a bricked phone is as good as a brick. So, if you end up bricking your phone, be creative and think of its uses as a […]