From 940a94afed1e7ae2de97bb8b06db551b521b0fe9 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 2 Jun 2019 15:29:07 -0400 Subject: [PATCH] Clean up --- deletefb/tools/common.py | 16 +++++++++------- deletefb/tools/likes.py | 9 +++++---- deletefb/tools/wall.py | 8 +++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index 06df691..cc9e95f 100644 --- a/deletefb/tools/common.py +++ b/deletefb/tools/common.py @@ -19,13 +19,15 @@ SELENIUM_EXCEPTIONS = ( TimeoutException ) -def try_move(actions, el): - for _ in range(10): - try: - actions.move_to_element(el).perform() - except StaleElementReferenceException: - time.sleep(5) - continue +def click_button(driver, el): + """ + Click a button using Javascript + Args: + driver: seleniumrequests.Chrome Driver instance + Returns: + None + """ + driver.execute_script("arguments[0].click();", el) def logger(name): """ diff --git a/deletefb/tools/likes.py b/deletefb/tools/likes.py index 1677979..9498f2e 100644 --- a/deletefb/tools/likes.py +++ b/deletefb/tools/likes.py @@ -2,9 +2,8 @@ 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 time import sleep -from .common import SELENIUM_EXCEPTIONS, archiver, logger +from .common import SELENIUM_EXCEPTIONS, archiver, logger, click_button LOG = logger(__name__) @@ -70,15 +69,17 @@ def unlike_page(driver, url): button = driver.find_element_by_xpath("//*[text()='Liked']") - driver.execute_script("arguments[0].click();", button) + # Click the "Liked" button to open up "Unlike" + click_button(driver, button) wait.until( EC.presence_of_element_located((By.XPATH, "//a/span[text()='Unlike']")) ) + # There should be an "Unlike" button now, click it unlike_button = driver.find_element_by_xpath("//a/span[text()='Unlike']/..") - driver.execute_script("arguments[0].click();", unlike_button) + click_button(driver, unlike_button) def unlike_pages(driver, profile_url): diff --git a/deletefb/tools/wall.py b/deletefb/tools/wall.py index 9983e41..f164305 100644 --- a/deletefb/tools/wall.py +++ b/deletefb/tools/wall.py @@ -1,13 +1,11 @@ import time - from selenium.webdriver.common.action_chains import ActionChains -from .common import SELENIUM_EXCEPTIONS, archiver +from .common import SELENIUM_EXCEPTIONS, archiver, click_button # Used as a threshold to avoid running forever MAX_POSTS = 15000 - def delete_posts(driver, user_profile_url, year=None): @@ -59,8 +57,8 @@ def delete_posts(driver, actions.move_to_element(delete_button).click().perform() confirmation_button = driver.find_element_by_class_name("layerConfirm") - # Facebook would not let me get focus on this button without some custom JS - driver.execute_script("arguments[0].click();", confirmation_button) + click_button(driver, confirmation_button) + except SELENIUM_EXCEPTIONS: continue else: