Daily Archives: March 31, 2016

Automate Facebook Login and Status Update with Python Selenium

You can download the source code from my GitHub page

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

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]")

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


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) \

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()

Step 10 – Close the browser