配置chromeOptions、appPackage、appActivity
可以使用命令 adb shell dumpsys activity activities | grep real 查看當前活躍的進程
'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand3'}, 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI',
配置platformName、deviceName、platformVersion
'platformName': 'Android', 'deviceName': 'HUAWEI Mate 9', 'platformVersion': '8.0.0',
注意:我的“智行火車票”是第一個小程序,寫代碼時根據小程序所在位置滑動點擊
使用uiautomatorviewer查看,查詢按鈕的座標位置
啓動Appium,運行python代碼,運行後手機界面停留在小程序智行火車票默認查詢起止地的車票信息頁面
python3代碼如下:
#!/usr/bin/env python3 # coding:utf-8 from appium import webdriver import time desired_caps = { 'platformName': 'Android', 'deviceName': 'HUAWEI Mate 9', 'platformVersion': '8.0.0', 'automationName': 'Appium', # 'unicodeKeyboard': True, # 'resetKeyboard': True, 'noReset': True, 'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand3'}, 'appPackage': 'com.tencent.mm', 'appActivity': '.ui.LauncherUI', } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) time.sleep(10) def swipeDown(driver, t=500, n=1): '''向下滑動屏幕''' l = driver.get_window_size() x1 = l['width'] * 0.5 # x座標 y1 = l['height'] * 0.25 # 起始y座標 y2 = l['height'] * 0.75 # 終點y座標 for i in range(n): driver.swipe(x1, y1, x1, y2, t) # 向下滑動 swipeDown(driver) time.sleep(2) # 點開小程序 driver.find_elements_by_id("com.tencent.mm:id/uk")[0].click() time.sleep(4) print(driver.contexts) # 注意,這裏是不需要切換的,別踩坑了!!!!!! # driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') time.sleep(3) # 點擊查詢按鈕 driver.tap([(486, 975), (594, 1038)], 500)