
👉Facebook Login Automation Using Python Selenium (With Example Code)
Facebook Login Automation : Automating Facebook login using Python can simplify testing processes, save time, and improve efficiency. By using Selenium, a powerful web automation tool, you can control a browser, fill text fields, click buttons, and perform various web-based tasks.
In this tutorial, we’ll guide you through the complete process of automating Facebook login using Python Selenium.
👉Steps to Facebook Login Automation
Follow these steps to successfully log in to Facebook using Python and Selenium:
✅ Step 1: Install Selenium
✅ Step 2: Set Up the WebDriver
✅ Step 3: Open Facebook in the Browser
✅ Step 4: Locate Username and Password Fields
✅ Step 5: Enter Credentials and Click Login
👉Step 1: Install Selenium
To install Selenium, run the following command in your terminal:
bash
pip install selenium
👉Step 2: Set Up the WebDriver
You’ll need to download the appropriate WebDriver for your browser. For Firefox, download geckodriver from the official site. For Chrome, download chromedriver.
👉Step 3: Code to Automate Facebook Login
Here’s the complete Python Selenium script for Facebook login automation:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
# Step 1) Open Firefox
browser = webdriver.Firefox()
# Step 2) Navigate to Facebook
browser.get(“http://www.facebook.com”)
# Step 3) Locate and fill the Email/Phone field & Password
username = browser.find_element(By.ID, “email”)
password = browser.find_element(By.ID, “pass”)
login_button = browser.find_element(By.NAME, “login”)
# Step 4) Enter credentials
username.send_keys(“your_email@example.com”)
password.send_keys(“your_password”)
# Step 5) Click the Login button
login_button.click()
👉Step 4: Code Explanation
✅ from selenium import webdriver – Imports Selenium WebDriver.
✅ browser = webdriver.Firefox() – Initializes Firefox as the browser.
✅ browser.get(“http://www.facebook.com”) – Navigates to Facebook’s login page.
✅ find_element() – Finds HTML elements like textboxes and buttons using their IDs.
✅ send_keys() – Sends the provided text (email/password) to the located elements.
✅ login_button.click() – Clicks the login button to submit the form.
👉Step 5: Sample Output
When the code runs successfully:
✅ The Facebook page will open.
✅ Your email and password will be entered automatically.
✅ The login button will be clicked, and you will be logged in to Facebook.
👉Important Notes
🔹 Always ensure your browser and WebDriver versions are compatible.
🔹 Avoid storing sensitive data (like passwords) directly in the script; consider using environment variables for security.
🔹 Selenium supports various browsers like Chrome, Firefox, Edge, and more.