Automate Facebook Login and Status Update with Python Selenium

You can download the source code from my GitHub page
https://github.com/bikrammann/web_crawler/blob/master/Automate_Facebook_Login_and_Status_Update.py

Step 1 – Required imports


import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

Step 2 – Create Firefox object and use get method then sleep for 5 seconds


driver = webdriver.Firefox() # Firefox Browser
driver.get('http://www.facebook.com')
time.sleep(5)

To use Google Chrome with Selenium, First install Google Chrome Driver from ‘https://sites.google.com/a/chromium.org/chromedriver/’. Then replace ‘C:/chrome/chromedriver.exe’ with your chrome driver path


# Replace
driver = webdriver.Firefox() # Firefox Browser
# With
driver = webdriver.Chrome('C:/chrome/chromedriver.exe') # Chrome Browser

Step 3 – Locate email address and password field on Facebook Login Page


username = driver.find_element_by_name("email")
password = driver.find_element_by_name("pass")

Step 4 – Add values to email address and password fields

 
username.send_keys("[email protected]")
password.send_keys("password123")

Step 5 – Sleep for 1 second, then press the login button


time.sleep(1)
driver.find_element_by_id("loginbutton").click()

Step 6 – Locate the text box where it says “What’s on your mind?”


message = driver.find_element(By.XPATH, "//textarea[@name='xhpc_message']")

Step 7 – Click the text box area to get it in focus where it says “What’s on your mind?”


ActionChains(driver) \
    .key_down(Keys.CONTROL) \
    .click(message) \
    .key_up(Keys.CONTROL) \
    .perform()

Step 8 – Message for status update


message.send_keys("Ha Ha")

Step 9 – Press the post button to submit the status update


driver.find_element(By.XPATH, '//button[text()="Post"]').click()
time.sleep(5)

Step 10 – Close the browser


driver.close()

4 thoughts on “Automate Facebook Login and Status Update with Python Selenium

  1. wordpress web developer

    I do not even know how I finished up here, but I believed
    this publish was once great. I do not recognise who you are however certainly you’re going to a well-known blogger when you are not already.
    Cheers!

Comments are closed.