前言

本篇介绍如何在微信公众号上自动化测试,以操作我的个人公众号:yoyoketang为例,没关注的,先微信关注了,再跟着操作

环境准备:

python 3.6

appium 1.7以上版本

微信6.6.6

微信里面webview 57.0

电脑上chrome版本 66.0

appium-chromedriver路径下chromedriver 2.28

开启微信debug模式

1.以微信为例,webview的版本号可以用chrome浏览器查看到,由于微信用的是x5内核,跟其他app不太一样,这里需要先开启微信的debug模式

开启微信debug模式:在微信聊天界面输入:debugx5.qq.com,如何勾选"打开TBS内核Inspector调试功能

2.查看微信里面webview版本,直接在电脑chrome浏览器输入:chrome://inspect/#devices

再打开微信的公众号页面,刷新浏览器页面,就会出现webview版本号57.0

ChromeOptions

1.在appium中context的切换时,识别webview的时候, 把com.tencent.mm:tools的webview识别成com.tencent.mm的webview. 从而导致context切换失败。

所有这里必须加上这个参数ChromeOptions,这个是关键。

'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}

2.appium1.7以后的版本支持Uiautomator2了,为了保险一点,最好加上这个,用Uiautomator2执行

'automationName': 'Uiautomator2'

from appium import webdriver
import time # 作者:上海-悠悠 QQ交流群:512200893 desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': 'A5RNW18316011440',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'automationName': 'Uiautomator2',
'unicodeKeyboard': True,
'resetKeyboard': True,
'noReset': True,
'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
}

操作公众号

1.操作步骤如下(前提要先关注微信公众号:yoyoketang):

  • 点微信首页搜索按钮
  • 输入yoyoketang内容搜索
  • 点开公众号
  • 点公众号菜单-精品分类

2.代码实现

切换webview

# 切换到webview
time.sleep(2)
print(driver.contexts)
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')

打印结果:

['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools']

参考代码:

from appium import webdriver
import time # 作者:上海-悠悠 QQ交流群:512200893 desired_caps = {
'platformName': 'Android',
'platformVersion': '7.0',
'deviceName': 'A5RNW18316011440',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'automationName': 'Uiautomator2',
'unicodeKeyboard': True,
'resetKeyboard': True,
'noReset': True,
'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
} driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10) # 作者:上海-悠悠 QQ交流群:512200893 # 点微信首页搜索按钮
driver.find_element_by_accessibility_id("搜索").click()
# 输入内容搜索
time.sleep(3)
driver.find_element_by_id('com.tencent.mm:id/hx').send_keys("yoyoketang")
# 点开公众号
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') # 点webview上元素 目前有个问题会报找不到元素
driver.find_element_by_xpath(".//*[@id='namespace_1']/div[1]/div/div[2]").click()
time.sleep(2)
driver.quit()

在学习过程中有遇到疑问的,可以appium+python QQ群交流:330467341

appium+python自动化42-微信公众号的更多相关文章

  1. appium+python自动化42-微信公众号 (可能以后会遇到也遇到切换不了webview的问题 记录再此 还没试)

    前言 本篇介绍如何在微信公众号上自动化测试,以操作我的个人公众号:yoyoketang为例,没关注的,先微信关注了,再跟着操作 环境准备:python 3.6appium 1.7以上版本微信6.6.6 ...

  2. appium+python自动化43-微信公众号webview操作

    前言 上一篇已经解决切换到微信公众号的webview上了,但是定位webview上元素的时候一直提示找不到,打印page_source也找不到页面上的元素,这个问题困扰了一整天,还好最后找到了原因, ...

  3. python爬取微信公众号

    爬取策略 1.需要安装python selenium模块包,通过selenium中的webdriver驱动浏览器获取Cookie的方法.来达到登录的效果 pip3 install selenium c ...

  4. Python+Tornado开发微信公众号

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. 本教程针对的是已掌握Pyt ...

  5. 使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)

    前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...

  6. Python——WeRobot(微信公众号开发)

    模板消息接口 ''' 使用规则 1.所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限: 2.需要选择公众账号服务所 ...

  7. Python爬取微信公众号素材库

    这是我的之前写的代码,今天发布到博客园上,说不定以后需要用. 开始: #coding:utf-8 import werobot import pymongo   class Gongzhonghao( ...

  8. python之微信公众号开发(基本配置和校验)

    前言 最近有微信公众号开发的业务,以前没有用python做过微信公众号开发,记录一下自己的学习和开发历程,共勉! 公众号类型 订阅号 普通订阅号 认证订阅号 服务号 普通服务号 认证服务号 服务方式 ...

  9. appium+python自动化-微信公众号webview操作

    前言 上一篇已经解决切换到微信公众号的webview上了,但是定位webview上元素的时候一直提示找不到,打印page_source也找不到页面上的元素,这个问题困扰了一整天,还好最后找到了原因, ...

随机推荐

  1. 201621123010《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

  2. php session和cookie知识

  3. python 学习日志

    1.pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloa ...

  4. 算法训练 P1102

      算法训练 P1102   时间限制:1.0s   内存限制:256.0MB      定义一个学生结构体类型student,包括4个字段,姓名.性别.年龄和成绩.然后在主函数中定义一个结构体数组( ...

  5. 算法训练 Tricky and Clever Password

     算法训练 Tricky and Clever Password   时间限制:2.0s   内存限制:256.0MB      问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人 ...

  6. Java各种排序算法

      Java各种排序算法详解 排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有 ...

  7. matplotlib 操作子图(subplot,axes)

    Matplotlib 中文用户指南 3.3 使用 GridSpec 自定义子图位置 ax:matplotlib.axes._subplots.AxesSubplot,的基本操作 ax.set_xtic ...

  8. Texas Instruments matrix-gui-2.0 hacking -- app_description.php

    <?php /* * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ * * * Redistrib ...

  9. [LeetCode&Python] Problem 589. N-ary Tree Preorder Traversal

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

  10. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...