Facebook Přihlášení pomocí Python: Příklad přihlášení na FB

⚡ Chytré shrnutí

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.
  • 🦊 Otevřít prohlížeč: webový ovladač.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.
  • 🤖 Pomoc s umělou inteligencí: Nástroje umělé inteligence, jako je Copilot, generují Selenium automation scripts instantly.

Facebook Přihlášení pomocí Python

Chcete-li se přihlásit na Facebook pomocí Python, musíte použít Selenium (nástroj pro automatizaci webu). Selenium může automatizovat a ovládat prohlížeč a klikat, vyplňovat text, odesílat tlačítka, která se objevují na různých webových stránkách.

Jak se přihlásit na Facebook pomocí Python

Pro přihlášení na Facebook použijeme a Python Skript, který řídí Selenium, Selenium Python Skript bude

  • Krok 1) Otevřete Firefox
  • Krok 2) Přejděte na Facebook
  • Krok 3) Vyhledejte a zadejte pole E-mail nebo Telefon a zadejte heslo
  • Krok 4) Klikněte na Přihlásit

Zde je rychlé video o tom, že systém bude fungovat.

Poznámka: Můžete nakonfigurovat Selenium používat jakýkoli prohlížeč, jako je Chrome, Safari, IE atd. V tomto tutoriálu budeme používat FireFox

Co potřebujete k instalaci?

  • Ujistěte se, že máte Selenium nainstalovaný na vašem PC. Podívejte se na toto https://trials.autocruitment.com naučit se instalovat Selenium
  • Použijte tento odkaz k instalaci Python for Selenium

Code přihlásit se na Facebook pomocí 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()

Facebook Přihlášení pomocí Python

Vysvětlení kódu

  • Code linka 1: Z modulu selen importujte webový ovladač
  • Code linka 2: Z importu selenových modulů Keys
  • Code linka 4: V tomto řádku inicializujeme „FireFox“ tím, že z toho uděláte předmět.
  • Code linka 6Metoda „browser.get“ přejde na stránku zadanou URLWebDriver počká, až se stránka úplně načte (tj. dokud se neukončí událost „onload“), než vrátí řízení vašemu testu nebo skriptu.
  • Code linka 8: V tomto řádku najdeme prvek textového pole, kam se má napsat „e-mail“.
  • Code linka 9: V tomto řádku najdeme prvek textového pole, kam se má napsat „heslo“.
  • Code linka 10: V tomto řádku najdeme prvek tlačítka Odeslat, na který musíme kliknout
  • Code linka 11: Nyní posíláme hodnoty do sekce email
  • Code linka 12: Odeslání hodnot do sekce hesla
  • Code řádek 14: Klikněte na tlačítko „Odeslat“

Ukázkový výstup

Zadané hodnoty uživatelského jména „guru99“ a hesla.

Facebook Přihlášení pomocí Python

Facebooková stránka se přihlásí pomocí e-mailu a hesla. Stránka otevřena (viz obrázek níže)

Facebook Přihlášení pomocí Python

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()

Doporučené postupy a upozornění

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

Nejčastější dotazy

Použijte 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.

Potřebujete Pythonse Selenium library, and a browser driver such as GeckoDriver for Firefox or ChromeDriver for Chrome. Install Selenium with pip install selenium.

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.

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.

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.

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.

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.

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.

Shrňte tento příspěvek takto: