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)
# 点微信首页搜索按钮
driver.find_element_by_accessibility_id("搜索").click()
# 输入内容搜索
time.sleep(3)
driver.find_element_by_id('com.tencent.mm:id/hx').send_keys("yx")
# 点开公众号
time.sleep(3)
driver.find_element_by_id('com.tencent.mm:id/l7').click()
# 点公众号菜单-精品分类
time.sleep(3)
driver.find_elements_by_id('com.tencent.mm:id/aaq')[0].click()
#不同点,切换到webview
time.sleep(2)
print(driver.contexts)
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
time.sleep(3)
#切换句柄
handles=driver.window_handles
driver.switch_to_window(handles[1])
driver.find_element_by_xpath(".//*[@id='namespace_1']/div[1]/div/div[2]").click()

猜你喜欢

转载自www.cnblogs.com/badbadboyyx/p/12202910.html
今日推荐