关键词搜索

源码搜索 ×
×

Python笔记-U2控件操作和操作超时

发布2020-07-22浏览1653次

详情内容

以豌豆荚为例:

普通的操作:

  1. import uiautomator2 as u2
  2. d = u2.connect_usb("8681-A01-0xc60fcc9d")
  3. d.app_start(package_name="com.wandoujia.phoenix2")
  4. print("step 1")
  5. # 通过多个条件来进行限定
  6. d(resourceId="android:id/text1", text="我的").click()
  7. print("step 2")

这里是可以的,但如果把“我的”改为“我的1”就会卡死:

通过timeout设置超时:

  1. import uiautomator2 as u2
  2. d = u2.connect_usb("8681-A01-0xc60fcc9d")
  3. d.app_start(package_name="com.wandoujia.phoenix2")
  4. print("step 1")
  5. # 通过多个条件来进行限定
  6. d(resourceId="android:id/text1", text="我的1").click(timeout=5)
  7. print("step 2")

这里可以看到UiObjectNotFoundError。

下面是使用click_exist

  1. import uiautomator2 as u2
  2. d = u2.connect_usb("8681-A01-0xc60fcc9d")
  3. d.app_start(package_name="com.wandoujia.phoenix2")
  4. print("step 1")
  5. # 通过多个条件来进行限定
  6. #d(resourceId="android:id/text1", text="我的1").click(timeout=5)
  7. #clicke_exists如果控件存在就点击,不存在就返回
  8. d(resourceId="android:id/text1", text="我的1").click_exists(timeout=5)
  9. print("step 2")

下面是通过exists属性判断控件是否存在:

  1. import uiautomator2 as u2
  2. d = u2.connect_usb("8681-A01-0xc60fcc9d")
  3. d.app_start(package_name="com.wandoujia.phoenix2")
  4. print("step 1")
  5. # 通过多个条件来进行限定
  6. #d(resourceId="android:id/text1", text="我的1").click(timeout=5)
  7. #clicke_exists如果控件存在就点击,不存在就返回
  8. #d(resourceId="android:id/text1", text="我的1").click_exists(timeout=5)
  9. #在操作之前,通过exists属性判断控件是否存在
  10. print(d(resourceId="android:id/text1", text="我的1").exists(timeout=5))
  11. print("step 2")

下面是获取控件信息:

这里用python获取最下面的android.widget.LinearLayout

  1. import uiautomator2 as u2
  2. d = u2.connect_usb("8681-A01-0xc60fcc9d")
  3. d.app_start(package_name="com.wandoujia.phoenix2")
  4. print("step 1")
  5. # 通过多个条件来进行限定
  6. #d(resourceId="android:id/text1", text="我的1").click(timeout=5)
  7. #clicke_exists如果控件存在就点击,不存在就返回
  8. #d(resourceId="android:id/text1", text="我的1").click_exists(timeout=5)
  9. #在操作之前,通过exists属性判断控件是否存在
  10. #print(d(resourceId="android:id/text1", text="我的1").exists(timeout=5))
  11. print(d(resourceId="com.wandoujia.phoenix2:id/a_4").child(className="android.widget.LinearLayout").child(resourceId="com.wandoujia.phoenix2:id/g8").count)
  12. for view in d(resourceId="com.wandoujia.phoenix2:id/a_4").child(className="android.widget.LinearLayout").child(resourceId="com.wandoujia.phoenix2:id/g8"):
  13. print(view.info)
  14. print("step 2")

运行截图如下:

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载