DEV Community

Cover image for Login Facebook using Python
oladejo abdullahi
oladejo abdullahi

Posted on • Updated on

Login Facebook using Python

How to login to facebook using python

one of the most amazing part of python is Automation and controlling the browser.

In this lesson, we will see how to log in to the Facebook account using Python with the power of a library called selenium.
Selenium is a python library that automates and controls browsers and all of its activities. We can easily write some codes in our way to control browser tasks with the help of selenium. Although it is for automating web applications for testing purposes.
so without wasting time what are those things needed and step to be taken?

firstly we need to install some libraries below

  1. selenium
  2. webDriver_manager for chrome or geckodriver for firefox.

After installation of the two libraries above then we can start the code.
But before we move to the code let me try to explain some function we are going to use in the program.

Why the selenium and chromeDriver

  • We need the selenium here to open the site we need to login to i.e Facebook.
  • we are going to use it to inspect elements across email box, password box, and login button by finding their id or class or any possible way.
  • we are going to use it to write in email box, password box, and click on the login button.

How to do the above task with codes

  • find_element_by_id() : A function provided by selenium module to find the element with the help of their id.
  • find_element_by_name() : A function provided by selenium module to find the element with the help of their name.
  • send_keys() : A function provided by selenium module also to write data or text into the box we use
  • webdriver.Chrome(): A function that will open new window of chrome
  • get() : A function we will used to open up the Facebook website
  • quit(): A function to close the browser when we are done.
  • sleep() : A function to delay the running of the script for some seconds. Necessary modules to be import and important data before coding we ned to import the following from their lib. webdriver from selenium sleep from time ChromeDriverManager from webdriver_manager.chrome Options from selenium.webdriver.chrome.options Also you will need to provide your username and password.

HERE IS THE CODE

from selenium import webdriver
from time import sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
usrname=input('Enter Your Username:')
password=input('Enter Your Password:')
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.facebook.com/') #go to facebook 
print ("Opened facebook") 
sleep(1) #delay the script for 1 second

username_box = driver.find_element_by_id('email') # find element with the id 'email' on facebook to get the usernameBox
username_box.send_keys(usrname) # write my username in to the box
print ("Your user name has been entered")
sleep(1)

password_box = driver.find_element_by_id('pass')# find the passwordBox
password_box.send_keys(password) # write my password in to the box
print ("you password has been entered")

login_box = driver.find_element_by_name('login')#find login button
login_box.click() #click the login button

print ("Done")
input('You can type quit to exist')
driver.quit()
print("Finished")
Enter fullscreen mode Exit fullscreen mode

Note: if you are going to use the above make sure you have google chrome installed. otherwise use geckodriver to subtitute chromedriver everywhere in the code if you are using firefox .Also you will need to input your 'username' or 'phoneNumber' and 'password'.
if you find this helpful please follow me and don't forget to like. you can comment below if you find anything wrong.
see you in the next lesson where we will be discussing 'how to message friend on facebook with python'. Happy new year in advance.

if you have any question don't hesitate to ask. chat me up on WhatsApp or Mail. Don't forget to follow me on Twitter so that you don't miss any of my articles.

Top comments (0)