2025年3月14日 星期五 甲辰(龙)年 月十三 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

appium之Android keycode实现模拟按键

时间:12-10来源:作者:点击数:20

来源

Android是自带一些按键操作,即使现在的手机使用从底部滑动,或者左滑返回,它的原始的按键如home,返回键都是存在的,可以直接调用

使用场景

模拟手机硬件信号,如电源、音量、明暗、键盘

原理

根据Android keycode对应的数字,调用Android密钥代码,实现模拟按键。

如何使用?

使用函数keyevent()或者函数press_keycode()

  • #导包
  • from appium import webdriver
  • import time
  • #准备自动化配置信息
  • desired_caps={
  • 'platformName':'Android',
  • 'plathformVersion':'10',
  • 'deviceName':'MIUI12.0.1',
  • 'appPackage':'com.hpbr.bosszhipin',
  • 'appActivity':'.module.launcher.WelcomeActivity',
  • 'noReset':True,
  • 'newCommandTimeout':6000,
  • 'automationName':'UiAutomator2',
  • 'skipServerInstallation':True
  • }
  • driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
  • driver.implicitly_wait(10)
  • #模拟打开通知栏
  • driver.open_notifications()
  • time.sleep(5)
  • driver.keyevent(4) #关闭通知栏--模拟发送返回键
  • # driver.press_keycode(4) 效果同上
  • time.sleep(2)
  • driver.keyevent(3) #返回到主屏幕,home键
  • input('按任意字符退出程序')
  • driver.quit()

下面网址是Android官网对于keyevent的介绍,可以找到按键对应的数字。

http://developer.android.com/reference/android/view/KeyEvent.html

因为获取官网keycode比较麻烦,分享一篇总结好的比较详细的Android keycode的总结:

https://www.cdsy.xyz/computer/programme/android/241210/cd64994.html

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐