Automated Post on Facebook Groups Using a Bot

The Art of Willingness
April 28, 2020
The Disruptors – Button Wallet
May 2, 2020

This is the first bot we are going to create and you as a reader are going to experience hands on and also be giving some basic definitions to some of the key elements used in it. We will be using python to create this bot. Just imagine you are in a situation where you want to post in multiple Facebook groups and you are just waiting for Facebook to reallow that feature or doing it manually. NO WAY! We are going to do it with a small script that we will be using to accomplish this task. LET’s get into that.

What is a bot?

A python bot can be simply defined as an automated script which can run explicitly after initiation. This can be coded in numerous languages albeit python is the most often used, hence we will also use it.

What is selenium?

Selenium is an open-source web-based automation tool. Python language is used to test and create personal testable scripts.

Pre-requisites:

  1. IDE: an integrated development environment of choice
  2. Python
  3. Selenium for python which can be installed using pip install
  4. Chrome driver: which will be an exe extension file
  5. Knowing basics of python coding
  6. Facebook credentials
  7. List of Facebook group URL’s
  8. You shall be member of those groups as most don’t allow everyone to post
from selenium import webdriver
from selenium.webdriver.common.by import By
import time


driver = webdriver.Chrome()
driver.maximize_window()
time.sleep(3)
driver.get("https://www.facebook.com")
# email (change ******* with your facebook email)
email_element = driver.find_element_by_xpath('.//*[@id="email"]')
email_element.send_keys('*******')
# password (change ***** with your facebook password)
password_element = driver.find_element_by_xpath('.//*[@id="pass"]')
password_element.send_keys('*****')
log_btm_element = driver.find_element_by_xpath('.//*[@id="loginbutton"]')
log_btm_element.click()
time.sleep(5)
# list of groups that you wanna post in (change *** with facebook group url)
groups_list = ["***", "***"]
for group in groups_list:
    try:
        driver.get(group)
        time.sleep(5)
        start_disc_element = driver.find_element_by_xpath('//*[@id="pagelet_group_composer"]')
        start_disc_element.click()
        time.sleep(10)
        status_element = driver.find_element(By.XPATH,'.//div[@contenteditable="true"]')
        # the status that you wanna post (change the *** with the status that you wanna post)
        status_element.send_keys('***')
        time.sleep(25)
        post_element = driver.find_element_by_xpath('''/html/body/div[1]/div[3]/div[1]/div/div[2]/
div[2]/div[2]/div[2]/div[3]/div[1]/div/div/div[2]/div[1]/div/div/div/div[2]
/div/div[3]/div[3]/div/div[2]/div/div[2]/button''')
        post_element.click()
        time.sleep(25)
    except:
        print(group)
	  # print the group URLs where post was unsuccessful

Here’s the complete code for sharing post on Facebook groups automatically just need to see for the green ‘*’ that need to be filled by reading the instructions given as comments incase anything is left unclear don’t hesitate to write to me in the comments. Once you are ready just run the code in any IDE of your choice and it should work wonders. This will open a chrome testing screen and you will see the screen work automatically leave it alone for a while depending on number of groups you want to share the post to.

We will try to cover some other basic aspects next time.

Lazy people create this when they are left idle to make sure the task is done and they are still free. If you have any questions suggestions or feedbacks feel free to write to me in the comments section. Until next time! STAY home STAY safe! Except if it’s an emergency. Thank you and enjoy.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

COVID19 INFO