Automate Facebook Posts Using Python
By: Abhay Parashar
Tags: Selenium, Python, Productivity, Automation, Web Scraping

1. Installation

Now to get started with the automation we need to download and install Python with some packages.

Download the latest version of python from here. Double click on the .exe file and install it.

Now Next Thing Is to Download a text editor. Go to google and download anyone you like my preference is vs code.

Now the final step is to Install Selenium. Just Open Your Terminal and type

pip install selenium ##Windows

pip3 install selenium ## MacOS and Linux

One More Thing That is Require with selenium is drivers. Just Go To the documentation and select your browser and then download drivers according to your browser version. In the blog, I am Using Chrome Drivers.

2. Targeting Script To the Login Account

Let’s start by importing all the libraries we need.

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

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.action_chains import ActionChains

import time

Now We Define a PATH variable that contains the path to the chrome driver.

PATH = 'chromedriver.exe' ##Same Directory as Python Program

Now Let’s Open Facebook Login Page Using The Driver.

3. Log in to the account

Now to automate login, We Use Selenium Click Event and send_keys function to pass the login credentials in the field and then we press the Login Button Using Same Click Event on the button but first we need to find something unique to identify the Email field and Password Field which we can find using inspect.

After Inspect we see that both email and password have a unique id as email and pass which we will use to click them and pass credentials. Same we will also find a unique identity for Login Button Also.

Now Let’s Create a Login Function To Login into your account.

def login(id,password):

email = driver.find_element_by_id("email")

email.send_keys(id)

Password = driver.find_element_by_id("pass")

Password.send_keys(password)

button = driver.find_element_by_id("u_0_b").click()

pass login("YOUR_FACEBOOK_ID","YOUR_FACEBOOK_PASSWORD")

4. Handling Allow Popup

After We Successfully Login Into Facebook account, There is a sudden pop-up occurs that says Facebook Wants to send you a notification. This Pop up can cause our automation to fail so we need to handle it.

We will Use Webdriver Options To Reject all the notification.

##### Handling of Allow Pop Up In Facebook

option = Options()

option.add_argument("--disable-infobars")

option.add_argument("start-maximized")

option.add_argument("--disable-extensions") # Pass the argument 1 to allow and 2 to block

option.add_experimental_option("prefs", {

"profile.default_content_setting_values.notifications": 2

}) driver = webdriver.Chrome(chrome_options=option, executable_path=PATH)

5. Clicking On The + Button and adding a New Post and Posting it

Now that we have successfully logged in to our account next step to post something. We will again follow the same and find a unique identity for the New Post Button and then click it.

After that, we will use Action Chains to select Post from the drop-down. THen Using Send Keys We will pass our message to post and then we will again use action chains to post our article.

Full Code: