---
description: 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 we
title: Facebook Login using Python: FB Login Example
image: https://www.guru99.com/images/facebook-login-using-python-1.png
---

 

[Skip to content](#main) 

**⚡ 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.

* 🌐 **Selenium:** Selenium automates a real browser to fill forms and click buttons.
* 🦊 **Open browser:** webdriver.Firefox() or Chrome() launches the browser from Python.
* 🔎 **Find fields:** find\_element(By.ID, …) locates the email, password, and login button.
* ⌨️ **Fill & submit:** send\_keys() types text and click() submits the login form.
* 🤖 **AI help:** AI tools like Copilot generate Selenium automation scripts instantly.

[ Read More ](javascript:void%280%29;) 

![Facebook Login using Python]()

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](https://www.guru99.com/installing-selenium-webdriver.html) to learn to install Selenium
* Use [this](https://www.guru99.com/selenium-python.html) 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()

[](https://www.guru99.com/images/1/102219%5F1457%5FFacebookLog1.png)

**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.

[](https://www.guru99.com/images/1/102219%5F1457%5FFacebookLog2.jpg)

The Facebook page will login with email and password. Page opened (see image below)

[](https://www.guru99.com/images/1/102219%5F1457%5FFacebookLog3.png)

### RELATED ARTICLES

* [Online Python Compiler (Editor / Interpreter / IDE) to Run Code ](https://www.guru99.com/execute-python-online.html "Online Python Compiler (Editor / Interpreter / IDE) to Run Code")
* [Python vs Ruby – Difference Between Them ](https://www.guru99.com/python-vs-ruby-difference.html "Python vs Ruby – Difference Between Them")
* [Python enumerate() Function: Loop, Tuple & String ](https://www.guru99.com/python-enumerate-function.html "Python enumerate() Function: Loop, Tuple & String")
* [\[::-1\] in Python with Examples ](https://www.guru99.com/1-in-python.html "[::-1] in Python with Examples")

## 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.

## FAQs

🌐 How do you log into Facebook using Python?

Use Selenium to open a browser with webdriver.Firefox(), navigate to Facebook, locate the email and password fields by ID, type your credentials with send\_keys(), and click the login button.

📦 What do you need to automate Facebook login?

You need Python, the Selenium library, and a browser driver such as GeckoDriver for Firefox or ChromeDriver for Chrome. Install Selenium with pip install selenium.

🔎 How does Selenium find the login fields?

Selenium locates elements by their HTML id. In Selenium 4, use find\_element(By.ID, ’email’) for the email box, and similar calls for the password and login button.

⏳ Why should you use explicit waits?

Explicit waits with WebDriverWait pause until an element loads before Selenium interacts with it. This prevents errors when the Facebook page loads slowly or elements appear late.

❓ What else can you use besides Selenium to log into Facebook?

You can use the official Facebook Login API to authenticate from your application. It is more stable and secure than browser automation for production integrations.

🤖 How can AI tools like GitHub Copilot help with Selenium?

AI assistants like GitHub Copilot autocomplete Selenium calls, generate locators and waits, and convert a described flow into a working browser automation script as you type.

🧠 Can AI automate writing browser automation scripts?

Yes. AI-powered tools can automate generating Selenium scripts, suggest robust element locators, and add error handling and waits so automated logins are more reliable.

🔒 Is it safe to automate Facebook login?

Automate only your own account and follow the site’s terms of service. Never hard-code passwords, and be ready to handle cookie banners or two-factor authentication prompts.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/facebook-login-using-python-1.png","url":"https://www.guru99.com/images/facebook-login-using-python-1.png","width":"700","height":"250","caption":"Facebook Login using Python","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/facebook-login-using-python.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/facebook-login-using-python.html","name":"Facebook Login using Python: FB Login Example"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/facebook-login-using-python.html#webpage","url":"https://www.guru99.com/facebook-login-using-python.html","name":"Facebook Login using Python: FB Login Example","dateModified":"2026-07-11T10:33:58+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/facebook-login-using-python-1.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/facebook-login-using-python.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/anna","name":"Anna Blake","description":"I'm Anna Blake, specializing in Python tutorials, offering clear and concise lessons to help you master Python programming efficiently.","url":"https://www.guru99.com/author/anna","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/anna-blake-author.png","url":"https://www.guru99.com/images/anna-blake-author.png","caption":"Anna Blake","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Facebook Login using Python: FB Login Example","description":"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 we","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/anna","name":"Anna Blake"},"dateModified":"2026-07-11T10:33:58+05:30","image":{"@id":"https://www.guru99.com/images/facebook-login-using-python-1.png"},"copyrightYear":"2026","name":"Facebook Login using Python: FB Login Example","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do you log into Facebook using Python?","acceptedAnswer":{"@type":"Answer","text":"Use Selenium to open a browser with webdriver.Firefox(), navigate to Facebook, locate the email and password fields by ID, type your credentials with send_keys(), and click the login button."}},{"@type":"Question","name":"What do you need to automate Facebook login?","acceptedAnswer":{"@type":"Answer","text":"You need Python, the Selenium library, and a browser driver such as GeckoDriver for Firefox or ChromeDriver for Chrome. Install Selenium with pip install selenium."}},{"@type":"Question","name":"How does Selenium find the login fields?","acceptedAnswer":{"@type":"Answer","text":"Selenium locates elements by their HTML id. In Selenium 4, use find_element(By.ID, 'email') for the email box, and similar calls for the password and login button."}},{"@type":"Question","name":"Why should you use explicit waits?","acceptedAnswer":{"@type":"Answer","text":"Explicit waits with WebDriverWait pause until an element loads before Selenium interacts with it. This prevents errors when the Facebook page loads slowly or elements appear late."}},{"@type":"Question","name":"What else can you use besides Selenium to log into Facebook?","acceptedAnswer":{"@type":"Answer","text":"You can use the official Facebook Login API to authenticate from your application. It is more stable and secure than browser automation for production integrations."}},{"@type":"Question","name":"How can AI tools like GitHub Copilot help with Selenium?","acceptedAnswer":{"@type":"Answer","text":"AI assistants like GitHub Copilot autocomplete Selenium calls, generate locators and waits, and convert a described flow into a working browser automation script as you type."}},{"@type":"Question","name":"Can AI automate writing browser automation scripts?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered tools can automate generating Selenium scripts, suggest robust element locators, and add error handling and waits so automated logins are more reliable."}},{"@type":"Question","name":"Is it safe to automate Facebook login?","acceptedAnswer":{"@type":"Answer","text":"Automate only your own account and follow the site's terms of service. Never hard-code passwords, and be ready to handle cookie banners or two-factor authentication prompts."}}]}],"@id":"https://www.guru99.com/facebook-login-using-python.html#schema-21745","isPartOf":{"@id":"https://www.guru99.com/facebook-login-using-python.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/facebook-login-using-python.html#webpage"}},{"embedUrl":"https://www.youtube.com/embed/9qZ60FFToao","name":"Facebook Login using Python: FB Login Example","description":"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 we","uploadDate":"2020-08-23T00:00:00+05:30","thumbnailUrl":"https://www.guru99.com/images/monday-com-logo.png","hasPart":[],"width":"560","height":"315","@type":"VideoObject","@id":"https://www.guru99.com/facebook-login-using-python.html#schema-507378","isPartOf":{"@id":"https://www.guru99.com/facebook-login-using-python.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"}]}
```
