Linked Questions

Popular Questions

Facebook API only allows you to fetch messages from a personal page and not an actual user.

import keyboard
from selenium import webdriver
from import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from import Options
from import By
import time
import cred

login_url = ''

class FacebookLogin():
    def __init__(self, email, password, browser):
        # Store credentials for login = email
        self.password = password
        if browser == 'Chrome':
            # Use chrome
            options = Options()

            self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        elif browser == 'Firefox':
            # Set it to Firefox
            self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
        time.sleep(1) # Wait for some time to load

    def login(self):
        email_element = self.driver.find_element(By.ID, 'email')
        email_element.send_keys( # Give keyboard input
        password_element = self.driver.find_element(By.ID, 'pass')
        password_element.send_keys(self.password) # Give password as input too
        login_button = self.driver.find_element(By.NAME, 'login') # Send mouse click
        convo_click = self.driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div/div[2]/div/div/div/div[1]/div[1]/div[1]/div/div[2]/div/div/div/div/div[3]/div/div/div/div/div[2]/div/div[1]/div/div[1]/a/div/div/div[2]/div/div/span/span/span')

        print("Press the 'Space' key to exit.")

if __name__ == '__main__':
    # Enter your login credentials here
    fb_login = FacebookLogin(, password=cred.password, browser='Chrome')

This code will allow you to log in and get to the first conversation.


Can be added to make it headless as follow

self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)

From there I tried to fetch upcoming messages through CSS_SELECTOR and XPATH but it doesn't work since they change with new coming messages.

I looked up github and saw a few posts but they're all old and not up to date.

Where should I go from there? This is my first personal project and I am relatively new to programming.

Related Questions