当前位置:   article > 正文

appium+python adb shell模拟点击事件(input tap)_adb shell input tap

adb shell input tap

前言

此篇文章大部分内容为转载,部分内容本人细化,加工!

appium有时候定位一个元素很难定位到,或者说明明定位到这个元素了,却无法点击,这个时候该怎么办呢?
求助大神是没用的,点击不了就是点击不了,appium不是万能的,这个时候应该转换思路,换其它的方法去点击,比如我们可以用adb去执行点击事件

input事件

1.先查看input事件语法有哪些,首先确保手机连上电脑(我这里是电脑上打开的手机模拟器)

  1. > adb device
  2. > adb shell
  3. > input
  1. C:\Users\Gloria>adb devices
  2. List of devices attached
  3. 127.0.0.1:52007 device
  4. C:\Users\Gloria>adb shell
  5. root@android:/ # input
  6. Usage: input [<source>] <command> [<arg>...]
  7. The sources are:
  8. trackball
  9. joystick
  10. touchnavigation
  11. mouse
  12. keyboard
  13. gamepad
  14. touchpad
  15. dpad
  16. stylus
  17. touchscreen
  18. The commands and default sources are:
  19. text <string> (Default: touchscreen)
  20. keyevent [--longpress] <key code number or name> ... (Default: keyboard)
  21. tap <x> <y> (Default: touchscreen)
  22. swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
  23. press (Default: trackball)
  24. roll <dx> <dy> (Default: trackball)
  25. root@android:/ #

2.input可以实现的功能

  • 输入文本信息:input text yoyo

  • 物理按键操作: input keyevent KEYCODE_VOLUME_DOWN

  • 点击(DOWN_UP)操作:input tap 500 500

  • 模拟滑动操作:input swipe 200 500 400 500

  • 模拟轨迹球操作 input roll 100 200

input tap

1.input tap实现的是DOWN_UP事件,也就是点击操作,后面两个参数是点击的坐标x,y

这里解释一下bounds值,工具中查看的bounds值是两个坐标,第一个[149,388]代表左上角的坐标;[290,618]代表右下角的坐标

2.举个例子:点击淘宝app的icon图标,先查看坐标[149,388][290,618],大概可以算出需要点击的点为(200, 500)  

这里再解释下(200,500)怎么来的,由上面的加黑字体解释可以得出,这个图标的中心位置就可以取两个x坐标的中点,和两个y坐标的中点;如149和290取中间的 200;388和618取中间的500。

 

打开cmd,输入adb执行,这个时候可以看到淘宝app已经启动了

> adb shell input tap 200 500

3.再继续点击淘宝上的‘我的淘宝’按钮 (600, 1250)

> adb shell input tap 600 1250

python执行adb命令

1.这里针对我的桌面布局,第一步点击百度阅读app,第二部点击我的进入“我的”页面

  1. #coding:utf-8
  2. from appium import webdriver
  3. import time
  4. import os
  5. #执行第一个adb command
  6. adb1 = 'adb shell input tap 150 1350' # 这里的坐标根据你的桌面app布局,我的是百度阅读
  7. os.system(adb1)
  8. time.sleep(5)
  9. #执行第二个adb comman
  10. adb2 = 'adb shell input tap 980 1810' # 进入”我的“页面
  11. os.system(adb2)

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56429
推荐阅读
相关标签
  

闽ICP备14008679号