From a26fcf42f3e6a4407c864088848d33341b9e74d0 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 26 May 2019 16:41:31 -0400 Subject: [PATCH] Unliking pages working --- deletefb/tools/common.py | 8 +++-- deletefb/tools/likes.py | 66 ++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index 4b6cbea..29d9341 100644 --- a/deletefb/tools/common.py +++ b/deletefb/tools/common.py @@ -1,9 +1,13 @@ -from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException +from selenium.common.exceptions import (NoSuchElementException, + StaleElementReferenceException, + TimeoutException) from time import sleep from json import dumps from os.path import abspath, relpath, split -SELENIUM_EXCEPTIONS = (NoSuchElementException, StaleElementReferenceException) +SELENIUM_EXCEPTIONS = (NoSuchElementException, + StaleElementReferenceException, + TimeoutException) def try_move(actions, el): for _ in range(10): diff --git a/deletefb/tools/likes.py b/deletefb/tools/likes.py index ed01fff..e74ec7a 100644 --- a/deletefb/tools/likes.py +++ b/deletefb/tools/likes.py @@ -1,10 +1,28 @@ -from time import sleep from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from .common import SELENIUM_EXCEPTIONS, archiver +def load_likes(driver): + """ + Loads the page that lists all pages you like + """ + driver.get("https://www.facebook.com/pages/?category=liked") + + wait = WebDriverWait(driver, 20) + + try: + wait.until( + EC.presence_of_element_located((By.XPATH, "//button/div/div[text()='Liked']")) + ) + + wait.until( + EC.presence_of_element_located((By.XPATH, "//button/div/i[@aria-hidden=\"true\"]")) + ) + except SELENIUM_EXCEPTIONS: + return + def unlike_pages(driver, user_profile_url): """ @@ -15,30 +33,40 @@ def unlike_pages(driver, actions = ActionChains(driver) - driver.get("https://www.facebook.com/pages/?category=liked") + load_likes(driver) - wait = WebDriverWait(driver, 20) + pages_list = driver.find_element_by_css_selector("#all_liked_pages") - wait.until( - EC.presence_of_element_located((By.XPATH, "//div[text()='Liked']")) - ) + actions.move_to_element(pages_list).perform() - sleep(10) + unlike_buttons = pages_list.find_elements_by_xpath("//button/div/div[text()='Liked']/../..") - pages_list = driver.find_element_by_css_selector("#all_liked_pages") - actions.move_to_element(pages_list).perform() - unlike_buttons = pages_list.find_elements_by_xpath("//button") + while unlike_buttons: + for button in unlike_buttons: + try: + if "Liked" in button.text: + page_name = button.find_element_by_xpath("./../..").text.split("\n")[0] + + print("Trying to unlike {0}".format(page_name)) + + driver.execute_script("arguments[0].click();", button) + + archive_likes(page_name) + + print("{0} was unliked".format(page_name)) + + except SELENIUM_EXCEPTIONS as e: + continue - for button in unlike_buttons: + load_likes(driver) try: - actions.move_to_element(button).perform() - page_name = button.find_element_by_xpath("./../..").text.split("\n")[0] - archive_likes(page_name) - - except SELENIUM_EXCEPTIONS as e: - print(e) - continue - print(button) + pages_list = driver.find_element_by_css_selector("#all_liked_pages") + actions.move_to_element(pages_list).perform() + unlike_buttons = pages_list.find_elements_by_xpath("//button") + if not unlike_buttons: + break + except SELENIUM_EXCEPTIONS: + break # Explicitly close the log file when we're done with it like_log.close()