python 快手模拟操作

时间:2024-03-09 20:34:25

我是在MUMU模拟器中运行的,打开快手并登录;

调用uiautomator获取布局元素坐标, 然后再按坐标点击

# -*- coding: utf-8 -*-
import os,re,time
import xml.etree.cElementTree as ETree

class Element(object):
    def __init__(self):
        self.tempFile = os.getcwd()
        self.pattern  = re.compile(r"\d+")

    def __uidump(self):
        os.popen("adb shell uiautomator dump /data/local/tmp/uidump.xml")
        time.sleep(1)
        os.popen("adb pull /data/local/tmp/uidump.xml " + self.tempFile)
        time.sleep(2)

    def __element(self, attrib, name):
        self.__uidump()
        tree = ETree.ElementTree(file=self.tempFile + "\\uidump.xml")
        treeIter = tree.iter(tag="node")
        for elem in treeIter:
            if elem.attrib[attrib] == name:
                bounds = elem.attrib["bounds"]
                coord  = self.pattern.findall(bounds)
                xpoint = (int(coord[2]) - int(coord[0])) / 2.0 + int(coord[0])
                ypoint = (int(coord[3]) - int(coord[1])) / 2.0 + int(coord[1])
                return xpoint, ypoint

    def __elements(self, attrib, name):
        list = []
        self.__uidump()
        tree = ET.ElementTree(file=self.tempFile + "\\uidump.xml")
        treeIter = tree.iter(tag="node")
        for elem in treeIter:
            if elem.attrib[attrib] == name:
                bounds = elem.attrib["bounds"]
                coord = self.pattern.findall(bounds)
                xpoint = (int(coord[2]) - int(coord[0])) / 2.0 + int(coord[0])
                ypoint = (int(coord[3]) - int(coord[1])) / 2.0 + int(coord[1])
                list.append((xpoint, ypoint))
        return list
    def findElementByName(self, name):
        return self.__element("text", name)

    def findElementsByName(self, name):
        return self.__elements("text", name)

    def findElementByClass(self, clsname):
        return self.__element("class", clsname)

    def findElementsByClass(self, clsname):
        return self.__elements("class", clsname)

    def findElementById(self, id):
        return self.__element("resource-id", id)

    def findElementsById(self, id):
        return self.__elements("resource-id", id)

class Event(object):
    def __init__(self):
        process = os.popen("adb connect 127.0.0.1:7555")
        print(process.read())

    def touch(self, dx, dy):
        os.popen("adb shell input tap %s %s" % (str(dx), str(dy)))
        time.sleep(1)

def main():
    element = Element()
    event   = Event()

    while True:
        time.sleep(11)
        ele = element.findElementById(u"com.smile.gifmaker:id/left_btn")
        event.touch(ele[0], ele[1])
        time.sleep(1)
        ele = element.findElementById(u"com.smile.gifmaker:id/tab_name")
        event.touch(ele[0], ele[1])
        time.sleep(1)
        ele = element.findElementById(u"com.smile.gifmaker:id/follower")
        event.touch(ele[0], ele[1])
        time.sleep(11)
        ele = element.findElementById(u"com.smile.gifmaker:id/left_btn")
        event.touch(ele[0], ele[1])
        time.sleep(2)
        ele = element.findElementById(u"com.smile.gifmaker:id/left_btn")
        event.touch(ele[0], ele[1])
        time.sleep(1)

if __name__ == \'__main__\':
    main()