# v0.1 import time import config from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC friend_list = [] me_friend_page = "https://www.facebook.com/me/friends" def scroll_down(): SCROLL_PAUSE_TIME = 1 # Get scroll height last_height = browser.execute_script("return document.body.scrollHeight") while True: # Scroll down to bottom browser.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait to load page time.sleep(SCROLL_PAUSE_TIME) # Calculate new scroll height and compare with last scroll height new_height = browser.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height def get_friend_list(): # get list which contains multiple instances of class below find_href_list = browser.find_elements(By.XPATH, '//*[@class="oajrlxb2 g5ia77u1 qu0x051f esr5mh6w e9989ue4 r7d6kgcz rq0escxv nhd2j8a9 nc684nl6 p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso i1ao9s8h esuyzwwr f1sip0of lzcic4wl gmql0nx0 gpro0wi8"]') # extract each class instance from list for my_href in find_href_list: # get attribute href from each class - wa need friend links only # if link contains 3x char "/"add it to list - link is valid if my_href.get_attribute("href").count('/') == 3: friend_list.append(my_href.get_attribute("href")) return friend_list # disable pop-up window with block/allow notification options # https://stackoverflow.com/questions/38684175/how-to-click-allow-on-show-notifications-popup-using-selenium-webdriver options = webdriver.ChromeOptions() options.add_argument("--disable-notifications") s = Service(config.chrome_driver_path) browser = webdriver.Chrome(service=s, options= options) browser.get("https://facebook.com") # accept all cookies browser.find_element(By.XPATH, '//*[@class="_42ft _4jy0 _9xo7 _4jy3 _4jy1 selected _51sy"]').click() # enter username and password browser.find_element(By.ID, "email").send_keys(config.username) browser.find_element(By.ID, "pass").send_keys(config.password) # click login button browser.find_element(By.NAME, "login").click() # wait for the main page to load -check if field 'what is on your mind' is presented WebDriverWait(browser, 20).until( EC.presence_of_all_elements_located((By.XPATH, '//*[@class="a8c37x1j ni8dbmo4 stjgntxs l9j0dhe7"]'))) # click friends link browser.get(me_friend_page) # wait until New message icon is presented WebDriverWait(browser, 20).until( EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[aria-label='New message']"))) # scroll down to bottom of page so all friends can be collected scroll_down() friend_list = get_friend_list()