From 267d87e43de24b77986d56f238c5617887d0cac2 Mon Sep 17 00:00:00 2001 From: zhangkai <2403741920@qq.com> Date: Wed, 5 Nov 2025 14:48:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=97=AE=E9=A2=98=EF=BC=9A?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=8F=AF=E8=83=BD=E5=8D=A1=E4=BD=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8CAI=E6=A3=80=E6=B5=8B=E5=88=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=B8=8D=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utils/ControlUtils.py | 46 ++++++++++++++++++++++++++++++++++++++ script/ScriptManager.py | 49 +++++++++++++++-------------------------- 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/Utils/ControlUtils.py b/Utils/ControlUtils.py index bc02dbc..ea20697 100644 --- a/Utils/ControlUtils.py +++ b/Utils/ControlUtils.py @@ -104,6 +104,52 @@ class ControlUtils(object): print(e) return False + @classmethod + def isClickBackEnabled(cls, session: Client): + try: + + back = session.xpath( + # ① 常见中文文案 + "//*[@label='返回' or @label='返回上一屏幕']" + " | " + # ② 英文 / 内部 name / 图标 label 的按钮(仅限 Button,且可见) + "//XCUIElementTypeButton[@visible='true' and (" + "@name='Back' or @label='Back' or " # 英文 + "@name='返回' or @label='返回' or " # 中文 + "@label='返回上一屏幕' or " # 中文另一种 + "@name='returnButton' or" + "@name='nav_bar_start_back' or " # 内部常见 name + "(@name='TTKProfileNavBarBaseItemComponent' and @label='IconChevronLeftOffsetLTR')" # 你给的特例 + ")]" + ) + + if back.exists: + return True + elif session.xpath("//*[@name='nav_bar_start_back']").exists: + back = session.xpath("//*[@name='nav_bar_start_back']") + if back.exists: + return True + elif session.xpath( + "//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]").exists: + back = session.xpath( + "//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]") + if back.exists: + return True + elif session.xpath( + "(//XCUIElementTypeOther[@y='20' and @height='44']//XCUIElementTypeButton[@visible='true'])[1]").exists: + back = session.xpath( + "(//XCUIElementTypeOther[@y='20' and @height='44']//XCUIElementTypeButton[@visible='true'])[1]") + if back.exists: + return True + else: + return False + except Exception as e: + print(e) + return False + + + + # 点赞 @classmethod def clickLike(cls, session: Client, udid): diff --git a/script/ScriptManager.py b/script/ScriptManager.py index edf0deb..a6a3aa3 100644 --- a/script/ScriptManager.py +++ b/script/ScriptManager.py @@ -429,7 +429,7 @@ class ScriptManager(): LogManager.method_info("外层 while 检测到停止,即将 break", "关注打招呼", udid) break print("任务终止") - LogManager.method_error("greetNewFollowers 重试次数耗尽,任务终止", "关注打招呼", udid) + LogManager.method_error("greetNewFollowers任务终止", "关注打招呼", udid) # 关注打招呼 def greetNewFollowers(self, udid, needReply, isComment, needTranslate, event): @@ -461,8 +461,7 @@ class ScriptManager(): for i in range(count): LogManager.method_info(f"返回上一步", "关注打招呼", udid) session.appium_settings({"snapshotMaxDepth": 15}) - source = session.source() - LogManager.method_info(f"返回按钮的节点:{source}", "返回节点", udid) + ControlUtils.clickBack(session) event.wait(timeout=2) @@ -628,12 +627,18 @@ class ScriptManager(): self.comment_flow(filePath, session, udid, 100, 100) event.wait(timeout=2) + session.appium_settings({"snapshotMaxDepth": 12}) + + is_back_enabled = ControlUtils.isClickBackEnabled(session) + if not is_back_enabled: + print("返回失败") + raise Exception("返回失败,出现问题") + if count != 0: ControlUtils.swipe_up(client) # 右滑返回 # client.swipe_right() - session.appium_settings({"snapshotMaxDepth": 12}) back_btn = ControlUtils.clickBack(session) @@ -1163,7 +1168,6 @@ class ScriptManager(): '//XCUIElementTypeButton[.//XCUIElementTypeStaticText[@value="收件箱"]]' ) - print("el", el) if not el.exists: LogManager.method_error(f"检测不到收件箱", "检测消息", udid) raise Exception("当前页面找不到收件箱,重启") @@ -1249,14 +1253,7 @@ class ScriptManager(): user_text.tap() event.wait(timeout=3) - # xml = session.source() - # msgs = AiUtils.extract_messages_from_xml(xml) - # - # # 检测出对方发的最后一条信息, - # - # # 获取了最后一条消息 - # last_msg = next((item['text'] for item in reversed(msgs) if item['type'] == 'msg'), - # "") + xml = session.source() time.sleep(1) @@ -1293,17 +1290,7 @@ class ScriptManager(): LogManager.method_info(f"检测到对方最后发送的消息:{last_in}", "检测消息", udid) LogManager.method_info(f"检测我发送的最后一条信息:{last_out}", "检测消息", udid) - # 如果最后一条消息不是文字,随机取出一条当做最后一条消息,最后一条消息是last_msg_text - # isLanguage = AiUtils.is_language(last_msg) - # - # if isLanguage: - # last_msg_text = last_msg - # else: - # LogManager.method_info(f"对方发送的消息不是语言,随机挑选作为最后一条进行回复:{last_msg}", - # "检测消息", udid) - # # last_msg_text = random.choice(text_list) - # last_msg_text = last_msg # 获取主播的名称 # anchor_name = AiUtils.get_navbar_anchor_name(session) @@ -1347,7 +1334,6 @@ class ScriptManager(): sel = session.xpath("//TextView") if anchor_name not in anchorWithSession: - print("没有记忆") # 如果是第一次发消息(没有sessionId的情况) LogManager.method_info(f"第一次发消息:{anchor_name},没有记忆 开始请求ai", "检测消息", udid) @@ -1429,14 +1415,15 @@ class ScriptManager(): udid) sel.set_text(f"{aiResult or '暂无数据'}\n") - LogManager.method_info(f"存储的sessionId:{anchorWithSession}", "检测消息", udid) - event.wait(timeout=1) - # 返回 - ControlUtils.clickBack(session) + LogManager.method_info(f"存储的sessionId:{anchorWithSession}", "检测消息", udid) + event.wait(timeout=1) - # 重新回到收件箱页面后,强制刷新节点 - session.appium_settings({"snapshotMaxDepth": 25}) - event.wait(timeout=1) + # 返回 + ControlUtils.clickBack(session) + + # 重新回到收件箱页面后,强制刷新节点 + session.appium_settings({"snapshotMaxDepth": 25}) + event.wait(timeout=1) try: # 如果 2 秒内找不到,会抛异常