Python 简单的安卓操作

时间:2022-02-17 18:02:22

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

## image compare.
# 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))
#     # result bigger,the difference is more big
#     print(result)
# image_compare(r‘C:UsersmatsDesktopon.png‘,r‘C:UsersmatsDesktopof.png‘)


# d = Device(‘0185ca9b35780dc9‘)
# print(d.info)
# d.screen.on()

# if d.screen == "on":  # of d.screen != "off"
#     d.screen.off()
# elif d.screen == "off":  # of d.screen != "on"
#     d.screen.on()

# d.press.home()
# d.press.back()
# d.press("back")

# d.click(200,500)
# d.long_click(x, y)
# d(textContains=‘Bluetooth‘).click()
# d(text="Settings").click()
# d(text="Settings").long_click()

# d.swipe(100, 10, 100, 1400)

## Two point gesture ;   ((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")

## click a list object which has on/off button, so you choose its list first,then click its child object 开关按钮
# d(className="android.widget.LinearLayout", resourceId="com.android.settings:id/switch_bar") 
#   .child(className="android.widget.Switch") 
#   .click()

## select on/off button on the right side of "off", same as the above one. 开关按钮
# d(text="Off").right(className="android.widget.Switch").click()

## scroll to find some element,need to find the list first,then scroll in the list to find the child element 滚动查找
# d(className="android.widget.ListView", resourceId="android:id/list") 
#  .child_by_text(
#     "GMInfo3",
#     allow_scroll_search=True,
#     className="android.widget.TextView"
#   ).click()

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

## Set/Clear text of editable field  文本框改字
# d(text="fg").clear_text()
# d(text="To").set_text("My text...")  # set the 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")