分享

元素定位-查看app控件(weditor)

 小猪窝969 2021-08-06

我们可以借助Android SDK自的uiautomatorviewer查看元素,这就要求手机必须以USB的方式连接PC,我前面使用的是WIFI连接进行连接的。所以,openatx提供了另外一个工具weditor 来解决这个问题。

GitHub地址:https://github.com/openatx/weditor

(1)、安装:

pip install --pre --upgrade weditor
2)、使用
python3 -m weditor
3)、工具打开
默认会通过浏览器打开页面:http://atx.open./

(4)工具的操作步骤

选择android、输入手机或者模拟器的ip+端口,点击connect

dump hierarchy是用来刷新页面的

鼠标点击想要的元素,就可以查看他们的控件了

 

2、主要语法

 

(1)启动app

d.app_start("com.addcn.android.house591")

(2)关闭app
cls.d.app_stop("com.addcn.android.house591")

(3)ResourceId定位
cls.d(resourceId="com.addcn.android.house591:id/ad_banner").click()

(4)Text定位
d(text="精选").click()

(5)Description定位
d(description="..").click()

(6)ClassName定位
d(className="android.widget.TextView").click()

(7)xpath定位
d.xpath("//*[@content-desc='分享']").click()

(8)


3、其他操作
(1)#组默认元素等待超时(秒)
cls.d.wait_timeout = 20.0 #默认20

(2)元素拖拽
(3)开关点击
  • d(A).left(B), selects B on the left side of A.

  • d(A).right(B), selects B on the right side of A.

  • d(A).up(B), selects B above A.

  • d(A).down(B), selects B under A.

  • 例如:

    #选择“Wi-Fi”右侧的“开关” 
    d(text="Wi‑Fi").right(className="android.widget.Switch").click()

(4)获取/统计某个相同条件的数目
d(text="Add new").count
或者
len(d(text="Add new"))
得知数目之后,我们可以通过索引去定位
d(text="Add new")[0]d(text="Add new")[1]
也可以遍历
 
for view in d(text="Add new"):    view.info

(5)截图
#截取屏幕截图并保存到计算机上的文件中,要求Android> = 4.2。
d.screenshot( “ home.jpg ”) # get PIL.Image格式化图像。当然,你需要首先安装pillow  
image = d.screenshot() # default format =“pillow” image.save( “ home.jpg ”)#或home.png。目前,只有PNG和JPG支持

#得到OpenCV的格式图像。当然,你需要先安装numpy和cv2
import cv2
image = d.screenshot( format = ' opencv') cv2.imwrite( ' home.jpg '图像)#获取原始JPEG数据 imagebin = d.screenshot(格式= '原始') 打开( “ some.jpg ”, “ WB ”).WRITE(imagebin)

(6)手势操作
1、单击
d( text = “ Settings ”).click()

2、长按
d( text = “ Settings ”).long_click()


3、将对象拖向另一个点或另一个UI对象
#笔记:拖不能用于为Android <4.3。#将UI对象拖动到屏幕点(xy),0.5秒后 
dtext = “设置”).drag_to(x,y, duration = 0.5)#将UI对象拖动到另一个(中心位置) UI对象,在0.25dtext = “设置”).drag_to( text =  Clock ”, duration = 0.25)

4、在屏幕上滑动
# swipe from (sx, sy) to (ex, ey)d.swipe(sx, sy, ex, ey)
# swipe from (sx, sy) to (ex, ey) with 10 stepsd.swipe(sx, sy, ex, ey, steps=10)


5、在屏幕上拖拽
# drag from (sx, sy) to (ex, ey)d.drag(sx, sy, ex, ey)
# drag from (sx, sy) to (ex, ey) with 10 stepsd.drag(sx, sy, ex, ey, steps=10)




(7)获取对象信息和状态

1、

d(text="Settings").exists
#如果存在则为True,否则为假

or d.exists(text="Settings") # 进一步使用 d(text="Settings").exists(timeout=3) 
# 等待设置出现在3S,相同.wait(3)

2、检索特定UI对象的信息

d(text="Settings").info

3、获取/设置/清除可编辑字段的文本(例如,EditText小部件)
d(text = “ Settings ”).get_text()   # get widget text 
d(text = “ Settings ”).set_text(“ My text ... ”)   #设置文本 
d(text = “ Settings ”).clear_text( )   #清除文字、


(8)系统常用按键
# press home key
d.press.home()
# press back key
d.press.back()
# the normal way to press back key
d.press("back")----亲测可用
# press keycode 0x07('0') with META ALT(0x02) on
d.press(0x07, 0x02)
  • home                   #手机Home键

  • back                   #手机返回键

  • left                   #对应键盘上的向右键<-

  • right                  #对应键盘上的向右键->

  • up                    #对应键盘上的向上键

  • down                   #对应键盘上的向下键

  • center                  #选中?

  • menu                   #菜单

  • search                  #查找?

  • enter                  #对应键盘上的Enter键

  • delete(or del)                          #对应键盘上的DEL键 用于删除

  • recent(recent apps)                  #任务切换

  • volume_up                #声音向上调整

  • volume_down               #声音向下调整

  • volume_mute               #静音按键

  • camera                  #拍照

  • power                   #电源键

六、使用经验
1、使用前初始化
python -m uiautomator2 init
2、打开工具

python3 -m weditor

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多