diff --git a/worker/tasks/boss_recruit.py b/worker/tasks/boss_recruit.py index 82ea6c4..8e6fbf5 100644 --- a/worker/tasks/boss_recruit.py +++ b/worker/tasks/boss_recruit.py @@ -275,11 +275,25 @@ class BossRecruitHandler(BaseTaskHandler): if not input_box: return state + try: + input_box.click(by_js=True) + except Exception: + pass + + try: + input_box.clear() + except Exception: + pass + input_box.input(ASK_WECHAT_TEXT) state["asked_wechat"] = True time.sleep(random.randint(1, 3) + random.random()) - state["send_success"] = self._click_send_like_script(tab) + state["send_success"] = self._send_with_confirm( + tab, + input_box=input_box, + message=ASK_WECHAT_TEXT, + ) time.sleep(random.randint(1, 5) + random.random()) state["exchange_clicked"] = self._click_change_wechat_like_script(tab) @@ -290,15 +304,101 @@ class BossRecruitHandler(BaseTaskHandler): return state - def _click_send_like_script(self, tab) -> bool: - for selector in ('x://div[text()="发送"]', 'x://span[text()="发送"]'): - try: - btn = tab.ele(selector, timeout=1) - if btn: - btn.click() + def _click_send_like_script(self, tab, input_box=None) -> bool: + selectors = ( + 'x://div[text()="发送"]', + 'x://span[text()="发送"]', + 'x://button[normalize-space(.)="发送"]', + 'x://*[@role="button" and normalize-space(.)="发送"]', + ) + + for _ in range(3): + for selector in selectors: + try: + btn = tab.ele(selector, timeout=1) + if not btn: + continue + try: + btn.click(by_js=True) + except Exception: + btn.click() + time.sleep(0.25) return True + except Exception: + continue + time.sleep(0.25) + + if input_box: + try: + input_box.input("\n") + time.sleep(0.25) + return True except Exception: + pass + + return False + + def _send_with_confirm(self, tab, input_box, message: str, max_attempts: int = 2) -> bool: + """发送后检查末条消息,若未成功则自动补发。""" + msg = (message or "").strip() + if not msg: + return False + + for _ in range(max_attempts): + clicked = self._click_send_like_script(tab, input_box=input_box) + if not clicked: continue + if self._confirm_last_sent_message(tab, msg): + return True + + try: + input_box.click(by_js=True) + except Exception: + pass + try: + input_box.clear() + except Exception: + pass + input_box.input(msg) + time.sleep(0.35) + + return self._confirm_last_sent_message(tab, msg) + + def _confirm_last_sent_message(self, tab, message: str) -> bool: + """确认当前聊天窗口末条是否为刚发送内容。""" + msg = (message or "").strip() + if not msg: + return False + + target = re.sub(r"\s+", "", msg) + + try: + for _ in range(6): + items = tab.eles("css:.message-item", timeout=1) + if not items: + time.sleep(0.2) + continue + + for e in reversed(items[-6:]): + text = (e.text or "").strip() + if not text: + continue + + normalized = re.sub(r"\s+", "", text) + is_boss = False + try: + if e.ele("css:.item-boss", timeout=0): + is_boss = True + except Exception: + pass + + if is_boss and target in normalized: + return True + + time.sleep(0.2) + except Exception: + return False + return False def _click_change_wechat_like_script(self, tab) -> bool: