Skip to content

Instantly share code, notes, and snippets.

@neon-ninja
Created March 5, 2025 06:24
Automating FB login with Selenium, and solving captcha with automatic transcription
#!/usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
import requests
import re
import os
chrome_options = Options()
#chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(5)
driver.get('https://www.facebook.com/')
driver.find_element(By.NAME, 'email').send_keys(os.getenv("FB_EMAIL"))
driver.find_element(By.NAME, 'pass').send_keys(os.getenv("FB_PASS"))
driver.find_element(By.NAME, 'login').click()
print(driver.current_url)
if "two_step" in driver.current_url:
audio_url = driver.find_element(By.CSS_SELECTOR, 'a[href^="https://www.facebook.com/captcha/tfbaudio/player/"]').get_attribute("href").replace("/player", "")
print(f"AUDIO URL: {audio_url}")
audio_file = requests.get(audio_url).content
with open("captcha.mp3", "wb") as f:
f.write(audio_file)
result = requests.post("https://asr.auckland-cer.cloud.edu.au/asr?encode=true&task=transcribe&language=en&initial_prompt=Only%20return%20letters%20and%20numbers%2C%20not%20words&vad_filter=false&word_timestamps=false&output=txt", files={"audio_file": audio_file}).text.strip()
print(f"CAPTCHA: {result}")
result = re.sub('\W+','', result)
print(f"CAPTCHA: {result}")
driver.find_element(By.CSS_SELECTOR, "input[type=text]").send_keys(result)
driver.find_element(By.XPATH, "//*[contains(text(), 'Continue')]").click()
time.sleep(5)
print(driver.current_url)
assert driver.current_url == "https://www.facebook.com/"
print("Logged in")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment