selenium + python

in Technology

Automating facebook group posting using selenium webdriver and python

According to Snapagency, $75,000 a year, or 35% percent of all marketing spend on average for companies, almost cost a year of operation. The business owner should build an awareness for their product and service. High and deepen engagement about the business can help to create a continuous brand experience and increase brand visibility. There are many tools and channels to market their business nowadays. One of the methods is to post valuable content on Facebook. But how you can automate this process without wasting your time? You can use this script to automate posting to a specific Facebook group you are member. WARNING: PLEASE USE THIS SCRIPT WITH CAUTIOUS. Facebook will limit your posting if too much often. Take a break between posts and leave some time in between around 10 minutes.

First of all, install python 2.7 into your local machine. Then install selenium.

pip install selenium

Download the chrome driver here and place it in the same directory.

# -*- coding: utf-8 -*-

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def main():

	# Your Facebook account user and password
	usr = ""
	pwd = ""
	message = "How to learn code fast?\nhttp://mnurdin.com/2018/10/15/how-to-learn-coding-fast/"
	#set multiple fb groups here
	group_links = [
		"https://www.facebook.com/groups/OdysseyX"
	]

	chrome_options = webdriver.ChromeOptions()
	chrome_options.add_experimental_option("detach", True)
	chrome_options.add_argument("--disable-infobars")
	chrome_options.add_experimental_option("prefs", { \
		"profile.default_content_setting_values.notifications": 2 # 1:allow, 2:block 
	})

	driver = webdriver.Chrome(executable_path="/your_project_directory/chromedriver")
	driver.implicitly_wait(15) # seconds

	# Go to facebook.com
	driver.get("http://www.facebook.com")
	
	# Enter user email
	elem = driver.find_element_by_id("email")
	elem.send_keys(usr)
	# Enter user password
	elem = driver.find_element_by_id("pass")
	elem.send_keys(pwd)
	# Login
	elem.send_keys(Keys.RETURN)

	for group in group_links:

		# Go to the Facebook Group
		driver.get(group)

		# Click the post box
		post_box=driver.find_element_by_xpath("//*[@name='xhpc_message_text']")

		# Enter the text we want to post to Facebook
		post_box.send_keys(message)

		sleep(5)
		buttons = driver.find_elements_by_tag_name("button")
		sleep(5)
		for button in buttons:
			if button.text == "Post":
				button.click()
				sleep(10)

	# driver.close()

if __name__ == '__main__':
  main()

Credit to Jorge E. Hernández for his work.

If you looking for a consultation using this script, don’t hesitate to email me nurdin[a]intellij.my.

Write a Comment

Comment

  1. first, thanks your code
    I’ve tried this code
    But there was an error.
    i want to post that https://www.facebook.com/HotssTory/
    this facebook page is Anyone can post
    however, post_box=driver.find_element_by_xpath(“//*[@name=’xhpc_message_text’]”)
    There seems to be a problem with this part.
    i hope your help