In this post we will share the complete procedure to automatically posting the Birthday wishes on Facebook timeline of our friends. We need to implement below steps to fully automate the process.
- Python Selenium based script to Login to Facebook to and write Birthday wish.
- AWS account and Windows server to run the script developed in above step.
- Schedule the script to run on fix time using Windows task scheduler.
- Schedule the START and STOP of Windows EC2 server to save the compute cost using AWS Lambda and AWS CloudWatch.
STEP-1
Use the below Python Selenium based script. Below script uses the resources such as chromedrive.exe and pwd.txt file that are put n folder C:\\HappyFB. You can proceed with same folder structure or can change as per your convenience.
Import necessary classes from different modules from selenium import webdriver 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.common.exceptions import TimeoutException from selenium.webdriver.common.keys import Keys import time chrome_options = webdriver.ChromeOptions() path_to_chromedriver = "C:\HappyFB\chromedriver.exe" Disbale chrome popup notification. chrome_options.add_argument("--disable-notifications") prefs = {"profile.default_content_setting_values.notifications": 2} chrome_options.add_experimental_option("prefs", prefs) browser = webdriver.Chrome(path_to_chromedriver, chrome_options=chrome_options) open facebook.com using get() method browser.get('https://www.facebook.com/') user_name or e-mail id username = "akshaychauhan.simple@gmail.com" getting passowrd from text file with open('C:\HappyFB\pwd.txt', 'r') as myfile: password = myfile.read().replace('\n', '') print("Let's Begin") Search for EMAIL ID field element = browser.find_elements_by_xpath('//*[@id ="email"]') element[0].send_keys(username) print("Username Entered") Serach for Password Entry Field element = browser.find_element_by_xpath('//*[@id ="pass"]') element.send_keys(password) time.sleep(3) print("Password Entered") logging in log_in = browser.find_elements_by_id('loginbutton') log_in[0].click() print("Login Successfull") time.sleep(5) Traversing to Birthday Notification field browser.get('https://www.facebook.com/events/birthdays/') feed = 'Happy Birthday !' element = browser.find_elements_by_xpath("//*[@class ='enter_submit uiTextareaNoResize uiTextareaAutogrow uiStreamInlineTextarea inlineReplyTextArea mentionsTextarea textInput']") cnt = 0 for el in element: cnt += 1 element_id = str(el.get_attribute('id')) XPATH = '//*[@id ="' + element_id + '"]' post_field = browser.find_element_by_xpath(XPATH) post_field.send_keys(feed) post_field.send_keys(Keys.RETURN) print("Birthday Wish posted for friend" + str(cnt)) time.sleep(10) Close the browser browser.close()
The above script is also available on GitHub.
STEP-2
Now create a AWS account and launch the Windows EC2 using the below AMI. This AMI is preinstalled with Pyhton and Selenium. You can also use fresh AMI and then install Pyhton and Selenium.
Refer the below guide to create AWS account if you are new to AWS. Create AWS account.
When launching EC2 search and select the AMI ID: ami-00be6ecf9833e8717 as shown below.

STEP-3
After structuring the script and other required resources as below. Highlighted file are must have requirement.

Now open Windows Task Scheduler and schedule the script to run one time a day as per our convenience.
Select the highlighted option task create page.

Select the path of the batch script to set the action for task scheduler.

Select the time to set the trigger in task scheduler. Please keep in mind that you should set this time 5-10 minutes after the time that you will set in AWS Cloud Watch rules to start the instance.

Save the above settings and proceed to next step.
STEP-4
We will need to set two AWS Lambda functions-one two start the instance and second to stop the instance and their corresponding AWS Cloud Watch rules to trigger the function once in a day.
Download Python script to STOP and START the instance from GitHub. Enable the Lambda functions in AWS Lambda after downloading.

Set the rules in AWS CLoudWatch to trigger these lambda functions. Set the time in these rules as per your convenience. The time set here must be 5-10 minutes earlier than the time set in Windows Task Schedule r in STEP-3.

Now your script is ready to execute at the time set by you in Windows task scheduler. EC2 will start automatically at the time set by you in CloudWatch rules.
Please note that Facebook two factor authentication must be disabled in order to execute this script.
If you are new to AWS and want to get your hands dirty on this automation let us know.
Feel free to post your suggestion and improvement.