來源
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的介紹,可以找到按鍵對應的數字,不過需要FQ才能訪問。
http://developer.android.com/reference/android/view/KeyEvent.html
因為獲取官網keycode需要FQ比較麻煩,分享一篇別人總結好的比較詳細的Android keycode的總結: