python-uiautomator2/tasks/wechat/__init__.py

89 lines
2.5 KiB
Python

import logging
from uiautomator2 import Device
def click_search_btn(d: Device):
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/h5n"]')
if selector.exists:
selector.click()
else:
logging.warning("未找到组件")
d.press("back")
click_search_btn(d)
def input_search_text(d: Device, text: str):
clear_btn = d.xpath('//*[@resource-id="com.tencent.mm:id/mdd"]/android.widget.RelativeLayout[1]')
if clear_btn.exists:
clear_btn.click()
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/mdd"]')
if selector.exists:
selector.set_text(text)
else:
logging.warning("未找到组件")
def click_search_result(d: Device):
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/kbo"]')
selector.wait(timeout=5)
elements = selector.all()
print(elements)
if len(elements) > 0:
elements[0].click()
else:
logging.warning("未找到组件")
def input_chat_text(d: Device, text: str):
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bkk"]')
selector.wait(timeout=5)
if selector.exists:
selector.set_text(text)
else:
logging.warning("未找到组件")
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bql"]')
selector.wait(timeout=1)
selector.click()
def get_friend_text_msgs(d: Device):
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bp0"]/android.widget.RelativeLayout')
elements = selector.all()
msg = []
for item in elements:
# 好友消息
el = item.elem.xpath("android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView")
if len(el) > 0 :
msg.append(el[0].get("text"))
return msg
def get_last_friend_text_msg(d: Device):
msgs = get_friend_text_msgs(d)
return msgs[-1] if len(msgs) > 0 else None
def get_my_text_msgs(d: Device):
selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bp0"]/android.widget.RelativeLayout')
elements = selector.all()
msg = []
for item in elements:
# 自己的消息
el = item.elem.xpath("android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]android.widget.TextView")
if len(el) > 0 :
msg.append(el[0].get("text"))
return msg
def get_last_my_text_msg(d: Device):
msgs = get_friend_text_msgs(d)
return msgs[-1] if len(msgs) > 0 else None