python自动化:微信小程序

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()