zoukankan      html  css  js  c++  java
  • [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景

    如何理解uiautomator里面的 child, child_by_text, sibling,我们借助android原生的uiautomatorviewer抓取的控件来进行理解

    以如下图进行详细讲解(左边与右边的通过不同颜色进行圈起来,表示了这些控件的归属关系),例如红圈部分为一个父类,投影绿圈和黄圈均为它的子类(称为child),而绿圈和黄圈属于同级别关系,则可以理解为兄弟关系(称为sibling)

    比如要点击“Navigation bar hide”的开关进行打开或者关闭,则代码为:

    1 d(className = 'android.widget.LinearLayout').child_by_text('Adaptive brightness',className = 'android.widget.RelativeLayout').sibling(resourceId = 'android:id/switch_widget').click()

    按照代码从左往右执行的顺序,先找到父类的className,这时你会发现在该界面界面有很多个这样的控件,没办法进行唯一认定,然后再寻找该父类中的子类className精确的进行定位菜单选项,会发现其它父类中的子类也全部是一样,也还是没有办法进行唯一认定,然后就再加上子类中的某一个标题(这个是唯一的),所以这就需要使用 .child_by_text  的方法来进行定位选项,找到该选项后要对该选项进行开关操作,又因为该 child_by_text的子类跟所要点击的开头控件是是兄弟关系,所以就可以通过 .sibling方法进行进一步的精确定位。

    a、className = 'android.widget.LinearLayout'  :这个是父类
    b、className = 'android.widget.RelativeLayout' :这个是a步骤的子类,但跟d步骤同为兄弟关系
    c、'Adaptive brightness' :这个是b步骤的子类
    d、resourceId = 'android:id/switch_widget'  这个是a步骤的子类,但跟b步骤同为兄弟关系

    总结:该代码使用场景是在Android某一个界面中(比如设置界面),里面有多个选项要进行开关操作,就需要用到该方法,不然你无法操作其它开关选项。

    下面附上对该设置界面中两个开关进行操作的源码:

     1 #-*- coding:utf-8 -*-
     2 '''
     3 Created on 2018年7月27日
     4 
     5 @author: any
     6 '''
     7 from uiautomator import device as d
     8 while True:
     9     if d(className = 'android.widget.LinearLayout').child_by_text('Navigation bar hide',className = 'android.widget.RelativeLayout').sibling(resourceId = 'android:id/switch_widget'):
    10         d(className = 'android.widget.LinearLayout').child_by_text('Navigation bar hide',className = 'android.widget.RelativeLayout').sibling(resourceId = 'android:id/switch_widget').click()
    11     else:
    12         d(className = 'android.widget.LinearLayout').child_by_text('Navigation bar hide',className = 'android.widget.RelativeLayout').sibling(resourceId = 'android:id/switch_widget').click()
    13     if d(className = 'android.widget.LinearLayout').child_by_text('Adaptive brightness',className = 'android.widget.RelativeLayout').sibling(resourceId = 'android:id/switch_widget'):
    14         d(className = 'android.widget.LinearLayout').child_by_text('Adaptive brightness',className = 'android.widget.RelativeLayout').sibling(className = 'android.widget.Switch').click()
  • 相关阅读:
    90%的人不知道Python炫技的7个骚操作
    想查看微信好友撤回的消息?Python帮你搞定
    Python统计长字符串中字符频次【入门必学】
    用什么库写Python命令行程序?80%的人都用错了
    Python文件操作方法,看这篇就足够
    Python单例模式最佳实战【入门必学】
    学Python,连yield关键字你都不会用就太失败了吧?
    编译原理--引论(四)
    编译原理--引论(三)
    2012年英语一小作文
  • 原文地址:https://www.cnblogs.com/aziji/p/9379030.html
Copyright © 2011-2022 走看看