Hello everyone! In today’s article, we’ll be looking at using Python to automatically login to Facebook.
This will be a fun experiment that will give you a glimpse into web-browser automation using Python’s Selenium web driver. So let’s get right into the topic and create a script that will visit the Facebook page, enter the credentials, and log right in!
Prerequisites
Now, before going though this tutorial, you’ll need to install certain libraries in Python. These libraries will make it very easy for us to login to the browser.
We’ll be using the Selenium webdriver module in Python. This module enables us to control our web browser (Chrome / Firefox) using a driver program.
But, to use Selenium along with our browser, we’ll need to install the drivers for that browser (Chrome/Firefox). To install them, we’ll take the help of another Python module: webdriver_manager
Instead of having to download the selenium webdriver manually, you can simply import this module! This will fetch all requirements automatically for you.
So now, let’s pip install
the necessary packages, using the pip manager:
pip install selenium pip install webdriver_manager
Now that we’ve installed our requirements, let’s start writing the code!
Writing our Script to Automate Facebook login
Let’s first import the necessary modules. We’ll need selenium
as well as the webdriver_manager
.
from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager from webdriver_manager.chrome import ChromeDriverManager import time
Here, I need the webdriver
class of the core Selenium module. Also, since we’ll be using it with firefox/chrome, we’ll need to load the necessary WebDrivers.
Now, we’ll be using the below url to login:
LOGIN_URL = 'https://www.facebook.com/login.php'
Now, we’ll implement the login functionality as a class. Let’s call it FacebookLogin
.
When we call __init__()
, we’ll initialize the selenium webdriver session. We need to send both the email and password fields to our webdriver session, so we’ll take them as input.
Finally, we will fetch the LOGIN_URL
with a GET request from the webdriver.
class FacebookLogin(): def __init__(self, email, password, browser='Chrome'): # Store credentials for login self.email = email self.password = password if browser == 'Chrome': # Use chrome self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install()) elif browser == 'Firefox': # Set it to Firefox self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) self.driver.get(LOGIN_URL) time.sleep(1) # Wait for some time to load
Alright, now we have initialized the class instance. Now, to login, we’ll create another method called login()
to do this for us.
To login, we’ll need to give the input to the login elements (email
and pass
on the html page)
Selenium has the find_element_by_id()
method, which will automatically locate the corresponding element for you!
To send the keyboard input, we can use element.send_keys(input)
directly!
def login(self): email_element = self.driver.find_element_by_id('email') email_element.send_keys(self.email) # Give keyboard input password_element = self.driver.find_element_by_id('pass') password_element.send_keys(self.password) # Give password as input too login_button = self.driver.find_element_by_id('loginbutton') login_button.click() # Send mouse click time.sleep(2) # Wait for 2 seconds for the page to show up
Notice how simple the API is! We can directly do element.send_keys()
and element.click()
!
Finally, give the program some time to load the webpage, using time.sleep()
I’ll give you the full code below. Just make sure to use your proper login credentials in the main
module.
from selenium import webdriver from webdriver_manager.firefox import GeckoDriverManager from webdriver_manager.chrome import ChromeDriverManager import time LOGIN_URL = 'https://www.facebook.com/login.php' class FacebookLogin(): def __init__(self, email, password, browser='Chrome'): # Store credentials for login self.email = email self.password = password if browser == 'Chrome': # Use chrome self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install()) elif browser == 'Firefox': # Set it to Firefox self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) self.driver.get(LOGIN_URL) time.sleep(1) # Wait for some time to load def login(self): email_element = self.driver.find_element_by_id('email') email_element.send_keys(self.email) # Give keyboard input password_element = self.driver.find_element_by_id('pass') password_element.send_keys(self.password) # Give password as input too login_button = self.driver.find_element_by_id('loginbutton') login_button.click() # Send mouse click time.sleep(2) # Wait for 2 seconds for the page to show up if __name__ == '__main__': # Enter your login credentials here fb_login = FacebookLogin(email='sample@example.com', password='PASSWORD', browser='Firefox') fb_login.login()
Hopefully your browser will be showing your home page now. Hurray, you’ve successfully logged on to facebook!
Conclusion
In this article, we learned about using Python and Selenium to automate logging onto Facebook quickly!