From 3a816f9733dc46d0ab92b8728fc57d4247c1c24e Mon Sep 17 00:00:00 2001 From: break27 Date: Thu, 24 Jul 2025 11:21:58 +0800 Subject: [PATCH] update: mail content check --- 邮件批量发送脚本.py | 97 ++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 37 deletions(-) diff --git a/邮件批量发送脚本.py b/邮件批量发送脚本.py index 74d5c4a..c2ab5ae 100644 --- a/邮件批量发送脚本.py +++ b/邮件批量发送脚本.py @@ -6,7 +6,6 @@ import time from selenium import webdriver from selenium.common.exceptions import StaleElementReferenceException, TimeoutException from selenium.webdriver.common.action_chains import ActionChains -from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait @@ -146,11 +145,9 @@ def main(): except TimeoutException: continue except: break - def keyin(element: WebElement, string: str): - for char in string: - click(element) - element.send_keys(Keys.END) - element.send_keys(char) + def keyin(element: WebElement, value): + try: element.send_keys(value) + except: driver.execute_script(f"arguments[0].value = arguments[1];", element, value) def ready(driver, predicate): try: @@ -195,8 +192,10 @@ def main(): while True: try: driver.find_element(By.CSS_SELECTOR, "#io-ox-core") - print(f'[信息] 已登录') - break + loader = driver.find_element(By.CSS_SELECTOR, "#background-loader") + if not loader.is_displayed(): + print(f'[信息] 已登录') + break except: time.sleep(args.interval) @@ -226,7 +225,7 @@ def main(): if rate > 8.33: print('[警告] 当前发送速率已超出限制 8.33 封/分钟') print(f'[信息] 当前时区:{args.timezone}') - print(f'[信息] 已读取问候语 {len(greetings)} 条:', end='\n\n') + print(f'[信息] 已读取可用问候语 {len(greetings)} 条:', end='\n\n') for index, item in enumerate(greetings): print(f'\t[{index}] {item.get('locale')}', end=' ') @@ -255,6 +254,7 @@ def main(): global errors global sent + attempt = 0 current = index index += 1 @@ -282,6 +282,8 @@ def main(): while active: try: + clean = True + attempt += 1 print(f'[信息] 正在发送:{recipient}') click("button[aria-label='Edit copy']") @@ -295,8 +297,10 @@ def main(): case hour if 18 <= hour < 21: registry = selection.get('registry')[2] case _: registry = None - action = ActionChains(driver, 5000) - action.send_keys(registry or selection.get('default')) + clean = False + iframe = driver.switch_to.active_element + action = ActionChains(driver) + hello: str = registry or selection.get('default') if name is not None and (name := str(name).strip()) and not contains_non_latin_alphabet(name): const = config.CONSTANTS @@ -305,10 +309,14 @@ def main(): parts = HumanName(name, const) parts.capitalize() person = ' '.join(filter(None, [parts.title, parts.first or parts.middle or parts.last])) - action.send_keys(Keys.SPACE).send_keys(person) + hello = ' '.join([hello, person]) - action.send_keys(',') - action.perform() + hello += ',' + action.send_keys(hello).perform() + + if items := iframe.find_elements(By.XPATH, f'//*[contains(text(), "{hello}")]'): + target = items[0] + clean = target.text == hello driver.switch_to.default_content() wrapper = locate("div.io-ox-mail-compose-window div[data-extension-id='to'] > div.mail-input") @@ -317,35 +325,50 @@ def main(): # 填入收件人 click(wrapper) keyin(to, recipient) - # 发送邮件 - click("div.io-ox-mail-compose-window button[data-action='send']") - # 检测页面警告 - try: - wait = WebDriverWait(driver, timeout=args.interval) - alert = wait.until(lambda x: x.find_element(By.CSS_SELECTOR, "div.io-ox-alert.io-ox-alert-error")) - except TimeoutException: - sents[current] = '✔' - occurrence[0] += 1 - sent += 1 - break + if to.get_attribute('value') != recipient: + print(f'[警告] ({attempt}) 检测到收件人地址不正确,正在重试...') + elif not clean: + print(f'[警告] ({attempt}) 检测到邮件内容不正确,正在重试...') + else: + # 发送邮件 + click("div.io-ox-mail-compose-window button[data-action='send']") + # 检测页面警告 + try: + wait = WebDriverWait(driver, timeout=args.interval) + alert = wait.until(lambda x: x.find_element(By.CSS_SELECTOR, "div.io-ox-alert.io-ox-alert-error")) + + message = alert.text.replace('\n', ' ') + if not message: print(f'[警告] 程序异常。请保持页面在前台显示,避免最小化') + else: print(f'[警告] ({attempt}): {message}') - errors += 1 - message = alert.text.replace('\n', ' ') - print(f'[警告] 来自网页:{message}') - - # 关闭警告 - click("div.io-ox-alert.io-ox-alert-error button[data-action='close']") + # 关闭警告 + click("div.io-ox-alert.io-ox-alert-error button[data-action='close']") + except TimeoutException: + sents[current] = '✔' + occurrence[0] += 1 + sent += 1 + break while mails := driver.find_elements(By.CSS_SELECTOR, "div.io-ox-mail-compose-window"): - # 关闭过期邮件 - try: click("button[data-action='close']", parent=mails[0]) - except: continue - # 删除过期邮件 - click("div.modal-footer button[data-action='delete']") - break + try: + # 关闭过期邮件 + click("button[data-action='close']", parent=mails[0]) + # 删除过期邮件 + click("div.modal-footer button[data-action='delete']") + except: + continue + + if attempt < args.retry: + continue + else: + print('[警告] 已超出最大重试上限') + errors += 1 + break except KeyboardInterrupt: print('[信息] 程序中断') + active = False + break except Exception as e: print(f'[警告] 发生错误:{e}')