Coders Packet

Facebook sign in automation using Python and Selenium

By Vedant Keshav Jadhav

A demonstration of automating browser activity using Selenium and Python. Here We will automate the Facebook login by reading the username and password from a file using the Python Selenium module.

In this project, we will use Python and Selenium to automate Facebook sign given the username or email, and the password of a user in a file. Selenium is a tool that enables browser interaction and automation. It provides functionalities that emulate user interaction with the browser. Selenium is compatible with many languages and can be used for interaction with different browsers. In this project, we will be using Firefox.

Geckodriver: GeckoDriver is the link between Selenium tests and the Firefox browser. To work with Selenium and Firefox, geckodriver path is required in the project.

 

The username or email, and the passwords can be stored in a separate file by the users, or with a small modification can be hardcoded in the code itself. The format of storing must be:

Username : username_of_user
Password : password_of_user

 

The main tools we will be using from Selenium are:

1. webdriver- "WebDriver drives a browser natively, as a user would, either locally or on a remote machine using the Selenium server, marks a leap forward in terms of browser automation." - Selenium official site. It is the basic element required for us to automate the browser activity. The webdriver object natively interacts with browser and helps in carrying out various activities.

2. Keys- Keys emulate the keyboard or mouse events in browser. The main task here that we are doing is, entering the user details in the browser, hence, sending the keys.

 

The code for reading the data from file is -

def get_user_and_password(file):
    user, password = "", ""
    with open(file) as f:
        for line in f:
            words = line.strip().split(":")
            words = [i.strip().lower() for i in words]
            if words[0] == "username" or words[0] == "email":
                user += words[1]
            else:
                password += words[1]
    return user, password

 

Below is the code for  facebook log in using Selenium. Here we create the Selenium webdriver object, and pass the url of facebook in the get function. Next we find_by_element function to find the form element to send the username and password. The send_keys function emulates the typing on the browser.

def open_facebook(username, user_password):
    path = os.path.join(os.getcwd(), "geckodriver/geckodriver")
    browser = webdriver.Firefox(executable_path=path)
    browser.get("https://www.facebook.com/")
    user = browser.find_element_by_id("email")
    user.send_keys(username)
    sleep(5)
    password = browser.find_element_by_id("pass")
    password.send_keys(user_password)
    sleep(5)
    browser.find_element_by_id("u_0_b").click()

 

Overall final code for -

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
from time import sleep

def get_user_and_password(file):
    user, password = "", ""
    with open(file) as f:
        for line in f:
            words = line.strip().split(":")
            words = [i.strip().lower() for i in words]
            if words[0] == "username" or words[0] == "email":
                user += words[1]
            else:
                password += words[1]
    return user, password

def open_facebook(username, user_password):
    path = os.path.join(os.getcwd(), "geckodriver/geckodriver")
    browser = webdriver.Firefox(executable_path=path)
    browser.get("https://www.facebook.com/")
    user = browser.find_element_by_id("email")
    user.send_keys(username)
    sleep(5)
    password = browser.find_element_by_id("pass")
    password.send_keys(user_password)
    sleep(5)
    browser.find_element_by_id("u_0_b").click()


if __name__ == '__main__':
    file_path = os.path.join(os.getcwd(), "facebook_user_and_password.txt")
    username, user_password = get_user_and_password(file_path)

    open_facebook(username, user_password)

 

Download Complete Code

Comments

No comments yet