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 webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import cred

login_url = 'https://www.messenger.com/login'

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

            self.driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
            #options=options
        elif browser == 'Firefox':
            # Set it to Firefox
            self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
        self.driver.get(login_url)
        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(self.email) # 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')
        login_button.click() # Send mouse click
        
        time.sleep(6)
        
        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')
        convo_click.click()


        print("Press the 'Space' key to exit.")
        keyboard.wait('space')
        self.driver.quit()
 

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

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

options.add_argument("--headless=new")

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