From 2b1346720d5d32c5de50ee7c59cf56f2cc2b5b10 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Sun, 2 Jun 2019 02:24:45 -0400 Subject: [PATCH] Refactor page links --- deletefb/tools/likes.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/deletefb/tools/likes.py b/deletefb/tools/likes.py index be86926..c1cb508 100644 --- a/deletefb/tools/likes.py +++ b/deletefb/tools/likes.py @@ -32,6 +32,24 @@ def load_likes(driver, profile_url): LOG.exception("Traceback of load_likes") return + +def get_page_links(driver): + pages = driver.find_elements_by_xpath("//li//div/div/a[contains(@class, 'lfloat')]") + + actions = ActionChains(driver) + + return [page.get_attribute("href").replace("www", "mobile") for page in pages] + + +def unlike_page(driver, url): + driver.get(url) + + actions = ActionChains(driver) + + button_element = find_element_by_xpath("//span[text()='Unlike']/..") + actions.move_to_element(button_element).click().perform() + + def unlike_pages(driver, profile_url): """ Unlike all pages @@ -45,20 +63,14 @@ def unlike_pages(driver, profile_url): like_log, archive_likes = archiver("likes") - wait = WebDriverWait(driver, 20) - actions = ActionChains(driver) - load_likes(driver, profile_url) - pages = driver.find_elements_by_xpath("//li//div/div/a[contains(@class, 'lfloat')]") - - actions = ActionChains(driver) - - page_urls = [page.get_attribute("href").replace("www", "mobile") for page in pages] - - for url in page_urls: - driver.get(url) + urls = get_page_links(driver) + while urls: + for url in urls: + print(unlike_page(driver, url)) + urls = get_page_links(driver) # Explicitly close the log file when we're done with it like_log.close()