Python 简单的安卓操作

from uiautomator import Device
from PIL import Image
import math
import operator
from functools import reduce


# 比较图片
def image_compare(img1,img2):
    image1 = Image.open(img1)
    image2 = Image.open(img2)

    his1 = image1.histogram()
    his2 = image2.histogram()
    result = math.sqrt(reduce(operator.add,list(map(lambda a,b: (a-b)**2,his1,his2)))/len(his1))
    # 值越大,差异越大(不太准,图片稍有改动,值就上好几百了)
    print(result)
    
image_compare(r'C:\Users\wzt\Desktop\a.jpg',r'C:\Users\wzt\Desktop\c.jpg')


# 手机信息
d = Device('39504f3231593398')
print(d.info)


# 结果:
# {'currentPackageName': 'com.android.systemui', 'displayHeight': 2900, 'displayRotation': 0, 'displaySizeDpX': 360, 'displaySizeDpY': 740, 'displayWidth': 1440, 'productName': 'starqltezc', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}


# 手机亮屏/灭屏
d = Device('39504f3231593398')
d.screen.on()
d.screen.off()


# 按键
d.press("back")
# 可选项
# home, back, left, right, up, down, center, menu, search, enter,delete(or del), recent(recent apps), volume_up, volume_down,volume_mute, camera, power.


# 点击坐标
d.click(200,500)
d.long_click(x, y)


# 点击文本
d(textContains='Bluetooth').click()
d(text="Settings").click()
d(text="Settings").long_click()


# 滑屏
#(x1,y1,x2,y2)
d.swipe(100, 10, 100, 1400)


# 两指手势
# 两指缩放类似的功能。  ((sx1, sy1), (sx2, sy2)).to((ex1, ey1), (ex2, ey2))
d().gesture((200, 300), (300, 300)).to((100, 300), (900, 300))


# 拖拽
# d.drag(sx, sy, ex, ey)
d(text="Settings").drag.to(x, y, steps=100)
# drag the ui object to another ui object(center)
d(text="Settings").drag.to(text="Clock", steps=50)


# 截图
d.screenshot("home.png")


# 列表元素
# 点击子元素
d(className="android.widget.LinearLayout",resourceId="com.android.settings:id/switch_bar").child(className="android.widget.Switch").click()


# 列表滚动查找
d(className="android.widget.ListView", resourceId="android:id/list").child_by_text(
    "WiFi001",
    allow_scroll_search=True,
    className="android.widget.TextView"
  ).click()


# 元素是否存在
d(text="Settings").exists # True if exists, else False
d.exists(text="Settings") # alias of above property.


# 输入、清除文本
d(text="fg").clear_text()
d(text="To").set_text("My text...")


# 水平、竖直滚动
# scroll forward(default) vertically(default)
d(scrollable=True).scroll(steps=2)
# scroll forward horizentally
d(scrollable=True).scroll.horiz.forward(steps=100)
# scroll backward vertically
d(scrollable=True).scroll.vert.backward()
# scroll to beginning horizentally
d(scrollable=True).scroll.horiz.toBeginning(steps=100, max_swipes=1000)
# scroll to end vertically
d(scrollable=True).scroll.toEnd()
# scroll forward vertically until specific ui object appears
d(scrollable=True).scroll.to(text="Security")
posted @ 2020-01-17 19:29  wztshine  阅读(555)  评论(0编辑  收藏  举报