1. 確保手機已與電腦連接
2. 開啟微信debug模式
在微信聊天界面輸入:debugx5.qq.com,勾選"打開TBS內核Inspector調試功能”
3. 查看微信里面webview版本
在電腦chrome瀏覽器輸入:chrome://inspect/#devices,再打開微信的公眾號頁面,刷新瀏覽器頁面,就會出現webview版本號,點擊“inspect”即可定位元素
4. 下載對應版本的chromedriver
下載的對應版本的chromedriver替換該路徑:\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win下的chromedriver
5. 獲取公眾號的進程名字
adb shell
dumpsys activity top | grep ACTIVITY
ps 進程的pid
6. 實現代碼如下:
from appium import webdriver import time di = {} di['platformName'] = 'Android' di['platformVersion'] = '6.0' di['deviceName'] = 'Appium' di['unicodeKeyboard'] = True di['resetKeyboard'] = True di['appPackage'] = 'com.tencent.mm' di['appActivity'] = '.ui.LauncherUI' di['udid'] = '127.0.0.1:62001' #di['automationName']='uiautomator2' #不同點 di[ 'chromeOptions']="{'androidProcess': 'com.tencent.mm:tools'}" driver = webdriver.Remote('http://localhost:4723/wd/hub', di) time.sleep(5) driver.implicitly_wait(20) #向下滑動屏幕 def swipeDown(driver, t=500, n=1): l = driver.get_window_size() # x坐標 x1 = l['width'] * 0.5 # 起始y坐標 y1 = l['height'] * 0.25 # 終點y坐標 y2 = l['height'] * 0.75 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/r9")[0].click() time.sleep(4) # 注意,這里是不需要切換的,別踩坑了!!!!!! # driver.switch_to.context('WEBVIEW_com.tencent.mm:tools') # tap觸摸右下角那個菜單坐標 [873,1654], [1080,1861] driver.tap([(873,1654), (1080,1861)],500) # 點發紅包 driver.find_element_by_accessibility_id("發紅包").click()