diff --git a/Module/DeviceInfo.py b/Module/DeviceInfo.py index b0e8c85..76a30b0 100644 --- a/Module/DeviceInfo.py +++ b/Module/DeviceInfo.py @@ -82,7 +82,6 @@ class Deviceinfo(object): d.app_start(WdaAppBundleId) d.home() time.sleep(2) - # d.app_start(tikTokApp) target = self.relayDeviceScreenPort() self.pidList.append({ "target": target, diff --git a/Utils/AiUtils.py b/Utils/AiUtils.py index 661442f..4abbd3d 100644 --- a/Utils/AiUtils.py +++ b/Utils/AiUtils.py @@ -6,6 +6,7 @@ import numpy as np import wda from Utils.LogManager import LogManager import xml.etree.ElementTree as ET +from wda import Client # 工具类 class AiUtils(object): @@ -236,10 +237,46 @@ class AiUtils(object): and (e.get('visible') in (None, 'true')) ) + # 获取关注按钮 + @classmethod + def getFollowButton(cls, session: Client): + followButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]/Button[1]") + if followButton.exists: + print("找到了") + return followButton + else: + print("没找到") + return None + + # 查找发消息按钮 + @classmethod + def getSendMesageButton(cls, session: Client): + msgButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]") + if msgButton.exists: + print("找到了") + return msgButton + else: + print("没找到") + return None + # 获取当前屏幕上的节点 @classmethod def getCurrentScreenSource(cls): client = wda.USBClient("eca000fcb6f55d7ed9b4c524055214c26a7de7aa") print(client.source()) -# AiUtils.getCurrentScreenSource() \ No newline at end of file + + # 查找app主页上的收件箱按钮 + @classmethod + def getMsgBoxButton(cls, session: Client): + box = session.xpath("//XCUIElementTypeButton[name='a11y_vo_inbox']") + if box.exists: + return box + else: + return None + + # 获取收件箱中的未读消息数 + @classmethod + def getUnReadMsgCount(cls, session: Client): + btn = cls.getMsgBoxButton(session) + return cls.findNumber(btn.label) \ No newline at end of file diff --git a/Utils/ControlUtils.py b/Utils/ControlUtils.py index c5313ea..f14bd1d 100644 --- a/Utils/ControlUtils.py +++ b/Utils/ControlUtils.py @@ -53,15 +53,19 @@ class ControlUtils(object): @classmethod def clickBack(cls, session: Client): try: - x, y = AiUtils.findImageInScreen("back") - if x > 0: - r = session.swipe_right() - if r.status == 0: - return True - else: - return False + back = session.xpath("//*[@label='返回']") + if back.exists: + back.click() + return True + elif session.xpath("//*[@name='nav_bar_start_back']").exists: + back = session.xpath("//*[@name='nav_bar_start_back']") + back.click() + 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]") + back.click() + return True else: - print("本页面无法返回") return False except Exception as e: print(e) @@ -80,23 +84,6 @@ class ControlUtils(object): else: print("没有找到目标") return False - # 点击评论 - # @classmethod - # def clickComment(cls, session: Client): - # scale = session.scale - # x, y = AiUtils.findImageInScreen("add") - # print(x,y) - # if x > -1: - # print("找到目标了") - # r = session.click(x // scale, y // scale + 120) - # if r.status == 0: - # return True - # else: - # return False - # else: - # print("没有找到目标") - # return False - # 点击搜索 @classmethod @@ -110,6 +97,16 @@ class ControlUtils(object): print(e) return False + # 点击收件箱按钮 + @classmethod + def clickMsgBox(cls, session: Client): + box = session.xpath("//XCUIElementTypeButton[name='a11y_vo_inbox']") + if box.exists: + box.click() + return True + else: + return False + # 获取主播详情页的第一个视频 @classmethod def clickFirstVideoFromDetailPage(cls, session: Client): @@ -121,28 +118,5 @@ class ControlUtils(object): else: return False - # 获取关注按钮 - @classmethod - def clickFollowButton(cls, session: Client): - followButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]/Button[1]") - if followButton.exists: - print("找到了") - followButton.click() - return True - else: - print("没找到") - return False - # 查找发消息按钮 - @classmethod - def clickSendMesageButton(cls, session: Client): - msgButton = session.xpath("//Window[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[1]/Other[2]/Other[2]/Other[1]/Other[1]/Other[3]/Other[1]/Other[1]") - if msgButton.exists: - print("找到了") - print(msgButton.name) - msgButton.click() - return True - else: - print("没找到") - return False diff --git a/script/ScriptManager.py b/script/ScriptManager.py index c97e78a..44b71e8 100644 --- a/script/ScriptManager.py +++ b/script/ScriptManager.py @@ -196,24 +196,34 @@ class ScriptManager(): session.appium_settings({"snapshotMaxDepth": 15}) # 点击搜索按钮 ControlUtils.clickSearch(session) - # 查找输入框 - input = session.xpath('//XCUIElementTypeSearchField') - # 如果找到了输入框,就点击并且输入内容 - input.click() - # 稍作停顿 - time.sleep(1) # 返回上一步 - def goBack(): - # ControlUtils.clickBack(session) - client.swipe(5,120, 50, 120) - - # 删除数据 - removeModelFromAnchorList(anchor) - + def goBack(count): + # 如需回复消息 + if needReply: + count += 1 + for i in range(count): + ControlUtils.clickBack(session) + time.sleep(2) # 循环条件。1、 循环关闭 2、 数据处理完毕 while not event.is_set() or len(anchorList) > 0: + if needReply: + print("如果需要回复主播消息。走此逻辑") + if AiUtils.getUnReadMsgCount(session) > 0: + # 此处调用另外一个方法处理 + + # 设置查找深度 + session.appium_settings({"snapshotMaxDepth": 15}) + # 点击搜索按钮 + ControlUtils.clickSearch(session) + + # 查找输入框 + input = session.xpath('//XCUIElementTypeSearchField') + # 如果找到了输入框,就点击并且输入内容 + input.click() + # 稍作停顿 + time.sleep(1) # 获取一个主播 anchor = anchorList[0] @@ -229,16 +239,18 @@ class ScriptManager(): session.appium_settings({"snapshotMaxDepth": 25}) # 定位 "关注" 按钮 通过关注按钮的位置点击主播首页 - follow_button = session.xpath( - '//XCUIElementTypeCell[@index="1"]//XCUIElementTypeButton[@index="1"]').get() - if follow_button: + follow_button = session.xpath('//XCUIElementTypeOther[@name="zhang1231511"]//XCUIElementTypeButton[@enabled="true" and @visible="true"]') + time.sleep(2) + if follow_button.exists: print("找到关注按钮!") x = follow_button.bounds.x - 200 y = follow_button.bounds.y client.click(x, y) else: + goBack(1) + removeModelFromAnchorList(anchor) print("未找到关注按钮") - + continue time.sleep(3) # 找到并点击第一个视频 @@ -248,7 +260,8 @@ class ScriptManager(): def viewAnchorVideo(): print("开始查看视频") count = 2 - while count > 0: + while count != 0: + time.sleep(5) print("条件满足,继续查看") img = client.screenshot() time.sleep(1) @@ -256,44 +269,49 @@ class ScriptManager(): img.save(filePath) LogManager.info("保存屏幕图像成功", udid) print("保存了背景图") - time.sleep(1) + time.sleep(2) + # 查找add图标 addX, addY = AiUtils.findImageInScreen("add", udid) if addX != -1: r = ControlUtils.clickLike(session, udid) - # 如果点了赞,总数量-1 否则划下一个视频,无法点赞的原因很多。指不定是什么原因。所以直接下一个视频比较好 - if r: + # 点赞成功。总数量减1,否则就滑到下一个视频。原因太多。不好判断。不如直接下一个视频 + if r == True: count -= 1 else: client.swipe_up() else: + print("没找有效视频") client.swipe_up() - continue - - # 假装看几秒视频 - time.sleep(10) - client.swipe_up() - if count == 1: - client.swipe_right() break - # 点击第一个视频后的逻辑 - if cellClickResult: + # 假装看几秒视频 + time.sleep(5) + client.swipe_up() + + if count == 0: + ControlUtils.clickBack(session) + break + + # 如果打开视频失败。说明该主播没有视频 + if cellClickResult == True: print("点击了视频") - session.appium_settings({"snapshotMaxDepth": 5}) + session.appium_settings({"snapshotMaxDepth": 15}) + time.sleep(2) print("重新设置了匹配深度") viewAnchorVideo() time.sleep(2) print("视频看完了") session.appium_settings({"snapshotMaxDepth": 25}) - # 视频看完需要点关注。 - r = ControlUtils.clickFollowButton(session) - if r == False: - print("执行失败了") - - + # 点击关注按钮 + followButton = AiUtils.getFollowButton(session) + if followButton.exists: + followButton.click() + else: + goBack(2) + return time.sleep(2) - ControlUtils.clickFollowButton(session) + AiUtils.getSendMesageButton(session) time.sleep(2) # 查找聊天界面中的输入框节点 @@ -303,40 +321,31 @@ class ScriptManager(): # 准备发送一条信息 chatInput.click() time.sleep(2) - # text = random.choice(prologueList) - # msg = Requester.translation(text, anchorCountry) - chatInput.set_text("哈哈哈" + "\n") + text = random.choice(prologueList) + msg = Requester.translation(text, anchorCountry) + chatInput.set_text(msg + "\n") time.sleep(1) - - client.swipe_right() - time.sleep(1) - client.swipe_right() - time.sleep(2) else: - print("没找到") - client.swipe_right() - time.sleep(1) - client.swipe_right() - time.sleep(2) - break + print("无法发送信息,换个主播") + # 接着下一个主播 + goBack(3) - # 清除数据 - removeModelFromAnchorList(anchor) - # 流程结束 - # client.swipe_right() - # time.sleep(1) else: - print("获取该主播第一个视频失败") - goBack() + print("没找到主播的第一个视频") + # 删除当前数据 + removeModelFromAnchorList(anchor) + goBack(2) def test(self, udid): client = wda.USBClient(udid) - client.swipe_right() + session = client.session() + session.appium_settings({"snapshotMaxDepth": 10}) + print(client.source()) -manager = ScriptManager() -manager.test("eca000fcb6f55d7ed9b4c524055214c26a7de7aa") +# manager = ScriptManager() +# manager.test("eca000fcb6f55d7ed9b4c524055214c26a7de7aa") # manager.growAccount("eca000fcb6f55d7ed9b4c524055214c26a7de7aa")