diff --git a/main.py b/main.py index a0e22b3..ee962b4 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.remote.webelement import WebElement from common.utils import * from common.timer import Timer @@ -148,6 +149,14 @@ def main(driver: Chrome, logger = logging.getLogger('main')): def ready(driver: Chrome): busy = driver.find_element(By.CSS_SELECTOR, ".io-ox-busy") return busy + + def catch(predicate, force=True): + try: + wait = WebDriverWait(driver, timeout=parameters['interval']) + return wait.until(predicate, 'Timeout') + except Exception as e: + if force: raise e + return None try: driver.switch_to.new_window('tab') @@ -433,20 +442,22 @@ def main(driver: Chrome, logger = logging.getLogger('main')): progress['recipient'] = str(name) logger.info('[%d/%d] Sending to %s', index-1, limit-1, email) clean = True + error = None if (target := get_address()) != parameters['account']: - e = MismatchedEmailAddress(target) - raise e + error = MismatchedEmailAddress(target) + if not options.get('recovery'): raise error if (target := get_subject()) != subject: - e = MismatchedEmailSubject(target) - c = EC.presence_of_element_located - if not options.get('recover'): raise e - + error = MismatchedEmailSubject(target) + if not options.get('recovery'): raise error + + if error is not None and (c := EC.presence_of_element_located): + click("li[data-id='virtual/standard'] > .subfolders li:nth-child(6) .folder-arrow", condition=c) click("li[data-id='virtual/standard'] > .subfolders li:nth-child(6) li.folder:nth-child(1)", condition=c) - until(ready) + catch(ready, force=False) click("ul[aria-label='List view'] li[data-index='0']", condition=c) - until(ready) + catch(ready, force=False) click("ul.classic-toolbar button[aria-label='More actions']", condition=c) click(".abs + ul.dropdown-menu a[data-action='io.ox/mail/actions/copy']", condition=c) click(".modal-dialog ul.subfolders li[data-id='default0/Brouillons']", condition=c) @@ -454,10 +465,10 @@ def main(driver: Chrome, logger = logging.getLogger('main')): click("li[data-id='default0/Brouillons']") for _ in range(parameters['attempts']): - until(ready) + catch(ready, force=False) click("ul[aria-label='List view'] li[data-index='0']", condition=c) if get_subject() == subject: break - if not get_subject() == subject: raise e + if not get_subject() == subject: raise error flow.do(Wait) flow.react() @@ -542,8 +553,7 @@ def main(driver: Chrome, logger = logging.getLogger('main')): continue try: - w = WebDriverWait(driver, timeout=parameters['interval']) - alert = w.until(lambda x: x.find_element(By.CSS_SELECTOR, "div.io-ox-alert.io-ox-alert-error")) + alert: WebElement = catch(lambda x: x.find_element(By.CSS_SELECTOR, "div.io-ox-alert.io-ox-alert-error")) message = alert.text.replace('\n', ' ') cell(index, column.sent).value = '❌'