Facebook Login using Python: FB Login Example
โก Smart Summary
Python can log into Facebook automatically using Selenium, a browser automation tool. Here you will learn to write a Selenium script that opens a browser, navigates to Facebook, fills the email and password fields, and clicks login.

In order to log into Facebook using Python, you need to use Selenium (a web automation tool). Selenium can automate and control a browser and click, fill text, submit buttons that appear on various websites.
How to Login Facebook using Python
To log in to Facebook, we will use a Python Script that drives Selenium. The Selenium Python Script will
- Step 1) Open Firefox
- Step 2) Navigate to Facebook
- Step 3) Search & Enter the Email or Phone field & Enter Password
- Step 4) Click Login
Here is a quick video on the system will work.
Note: You can configure Selenium to use any browser like Chrome, Safari, IE, etc. In this tutorial, we will use FireFox
What do you need to Install?
- Ensure you have Selenium installed on your PC. Refer this link to learn to install Selenium
- Use this link to install Python for Selenium
Code to Login into Facebook using Python
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait # Step 1) Open Firefox browser = webdriver.Firefox() # Step 2) Navigate to Facebook browser.get("http://www.facebook.com") # Step 3) Search & Enter the Email or Phone field & Enter Password username = browser.find_element_by_id("email") password = browser.find_element_by_id("pass") submit = browser.find_element_by_id("loginbutton") username.send_keys("you@email.com") password.send_keys("yourpassword") # Step 4) Click Login submit.click()
Explanation of the code
- Code line 1: From selenium module import webdriver
- Code line 2: From selenium module import Keys
- Code line 4: In this line, we are initializing “FireFox” by making an object of it.
- Code line 6: The “browser.get method” will nagivagte to a page given by the URL. WebDriver wait until the page has been completely loaded (that is, the “onload” occasion has let go), before returning control to your test or script.
- Code line 8: In this line, we are finding the element of the textbox where the “email” has to be written.
- Code line 9: In this line, we are finding the element of the textbox where the “password” has to be written.
- Code line 10: In this line, we are finding the submit button element which we need to click
- Code line 11: Now we are sending the values to the email section
- Code line 12: Sending values to the password section
- Code line 14: Click on the “Submit” button
Sample Output
The values of the username “guru99” and password entered.
The Facebook page will login with email and password. Page opened (see image below)
Using Explicit Waits for a Reliable Login
Modern Selenium 4 replaces methods like find_element_by_id with find_element(By.ID, …). Combining this with an explicit wait makes the script wait for each field to load before interacting with it.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC browser = webdriver.Firefox() browser.get("https://www.facebook.com") wait = WebDriverWait(browser, 10) email = wait.until(EC.presence_of_element_located((By.ID, "email"))) password = browser.find_element(By.ID, "pass") email.send_keys("you@email.com") password.send_keys("yourpassword") browser.find_element(By.ID, "loginbutton").click()
The WebDriverWait pauses until the email field is present, which prevents errors when the page loads slowly.
Running the Script with Chrome
Selenium works with any browser. To use Chrome instead of Firefox, swap the driver and keep the rest of the code the same.
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get("https://www.facebook.com") browser.find_element(By.ID, "email").send_keys("you@email.com") browser.find_element(By.ID, "pass").send_keys("yourpassword") browser.find_element(By.ID, "loginbutton").click()
Best Practices and Cautions
- Automate only your own account and follow the website’s terms of service.
- Never hard-code passwords; read them from environment variables or a secrets manager.
- A real login may require handling a cookie banner or two-factor authentication.
- For production integrations, prefer the official Facebook Login API over browser automation.



