From d17919f6752500b425d91ef6435ab47579367317 Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Fri, 5 Jul 2019 03:57:08 -0400 Subject: [PATCH] Gathering conversations --- deletefb/tools/common.py | 16 ++++++++----- deletefb/tools/conversations.py | 40 +++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/deletefb/tools/common.py b/deletefb/tools/common.py index 7f6364a..5c14118 100644 --- a/deletefb/tools/common.py +++ b/deletefb/tools/common.py @@ -2,7 +2,8 @@ from os.path import isfile from selenium.common.exceptions import ( NoSuchElementException, StaleElementReferenceException, - TimeoutException + TimeoutException, + JavascriptException ) import json @@ -19,13 +20,18 @@ SELENIUM_EXCEPTIONS = ( 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 scroll_to(driver, el): + """ + Scroll an element into view, using JS + """ + try: + driver.execute_script("arguments[0].scrollIntoView();", el) + except SELENIUM_EXCEPTIONS: + return + def logger(name): """ Args: diff --git a/deletefb/tools/conversations.py b/deletefb/tools/conversations.py index cb023ae..45ebb7b 100644 --- a/deletefb/tools/conversations.py +++ b/deletefb/tools/conversations.py @@ -1,6 +1,6 @@ from .archive import archiver from ..types import Conversation -from .common import SELENIUM_EXCEPTIONS, logger, click_button +from .common import SELENIUM_EXCEPTIONS, logger, scroll_to from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait @@ -8,18 +8,44 @@ from selenium.webdriver.common.action_chains import ActionChains LOG = logger(__name__) -def get_conversation_list(driver): +def get_conversation_list(driver, offset=0): """ Get a list of conversations """ actions = ActionChains(driver) - convos = driver.find_elements_by_xpath("//ul[@aria-label=\"Conversation list\"]/li") + convos = driver.find_elements_by_xpath("//ul[@aria-label=\"Conversation list\"]/li/div/a[@role=\"link\"]") - for convo in convos: + for convo in convos[offset:]: actions.move_to_element(convo).perform() - yield convo.find_element_by_xpath("//a") + yield convo + actions.move_to_element(current_convo).perform() + +def get_all_conversations(driver): + conversation_urls = set() + + current_convo = None + + while True: + l = len(conversation_urls) + + for convo in get_conversation_list(driver, offset=l): + url = convo.get_attribute("data-href") + conversation_urls.add(url) + current_convo = convo + + if current_convo: + scroll_to(driver, current_convo) + + print(l) + print(len(conversation_urls)) + if len(conversation_urls) == l: + # no more conversations left + break + + return list(conversation_urls) + def delete_conversations(driver): """ @@ -30,5 +56,5 @@ def delete_conversations(driver): wait = WebDriverWait(driver, 20) - for convo_url in get_conversation_list(driver): - print(convo_url.get_property("data-href")) + for convo_url in get_all_conversations(driver): + print(convo_url)