appium+Python脚本编写

2018-12-04  本文已影响0人  websky

两年前刚进公司的时候用过appium,最后因换项目也就没再研究过了。就在前两个月一个通过阅读新闻赚钱的App让我意识到 也许appium可以这么干,哈哈。结果很不理想,这里就分享一下过程吧!

我选择的方案是 appium1.8.1 + 夜神模拟器 + Python脚本

准备工作

安装夜神模拟器

安装过程就不说了,需要注意的是在环境变量添加 夜神模拟器adb的路径。


image.png

然后启动模拟器,再打开命令行输入 adb devices

adb devices
List of devices attached
127.0.0.1:62001 device

image.png

有设备列表就对了 62001 就是模拟器的端口,可以通过adb执行命令,appium也是通过这个来连通设备的

安装Appium

准备材料

Appium国内下载地址
Appium中文文档
Appium各种资料 来自testerhome

我安装的是1.8.1 安装过程略过
配置环境变量

H:\soft\Appium\node_modules\.bin
image.png

然后安装nodejs ,配置jdk,Android SDK ,Windows下这些软件的安装过程都略过

Appium原理

Appium原理解析

Appium启动参数

开始

下载趣头条apk文件,下载apktool工具其实就是一个jar包
将这两个文件放在同一个文件夹(自己视情况而定),在当前文件夹打开命令行,使用apktool反编译趣头条


image.png

反编译命令

java -jar .\apktool_2.3.3.jar d .\qukan.apk
image.png

完成后会多一个目录,用编辑器打开里面的AndroidManifest.xml
要找的Appium启动app需要的两个参数appPackage,appActivity

image.png image.png

启动Appium服务

没什么特别的直接打开使用默认端口就行,如果被占用就改一下


image.png

然后打开一个inspector session


image.png

界面如下


image.png

输入启动参数,可以键值对,也可以直接编辑右边的json数据,我还是觉得json快一些

image.png

启动后是这样的


image.png image.png

这些功能用了选取元素。

下面是我之前做好的一下分析:

1:启动会出现红包挂件 --> 挂件信息 如下
Attribute       Value
index           0
class           android.widget.LinearLayout
package         com.jifen.qukan
content-desc    
checkable       false
checked         false
clickable       false
enabled         true
focusable       false
focused         false
scrollable      false
long-clickable  false
password        false
selected        false
bounds          [171,372][548,945]
resource-id 
instance        0

判断 -> (如果有挂件){
    通过点击关闭红包挂件

}

关闭按键信息为:

Attribute   Value
index           1
text            先去逛逛
class           android.widget.TextView
package         com.jifen.qukan
content-desc    
checkable       false
checked         false
clickable       true
enabled         true
focusable       false
focused         false
scrollable      false
long-clickable  false
password        false
selected        false
bounds          [171,913][548,945]
resource-id     com.jifen.qukan:id/a0d
instance        5

2:开始读取文章列表

文章页面又分为两部分:
    第一部分 ---- 新闻分类:         资源:ID  com.jifen.qukan:id/qk -> class:android.widget.LinearLayout
                    分类标题:      对应为:[class   android.widget.TextView] 数组

    第二部分 ---- 主体内容:        resource-id  com.jifen.qukan:id/kt 
                    新闻列表:              [主体内容中子元素] -> com.jifen.qukan:id/uo

3,点击进入新闻页面:(判断有无红包挂件){    resource-id  com.jifen.qukan:id/a86
    有 则写上下滑动手势 获取金币
    }else{
        返回到列表,阅读下一个
    }

编写python脚本

安装Appium-Python-Client

pip install Appium-Python-Client

下面是我写的脚本代码

from appium import webdriver
from time import sleep

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.2'
desired_caps['deviceName'] = '127.0.0.1:62001 device'
desired_caps['appPackage'] = 'com.jifen.qukan'
desired_caps['appActivity'] = 'com.jifen.qkbase.view.activity.JumpActivity'
desired_caps['noReset'] = 'true'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

def swipeUp(driver, t=500, n=1):
    '''向上滑动屏幕'''
  l = driver.get_window_size()
    x1 = l['width'] * 0.5 # x坐标
  y1 = l['height'] * 0.75 # 起始y坐标
  y2 = l['height'] * 0.25 # 终点y坐标
  for i in range(n):
        driver.swipe(x1, y1, x1, y2, t)

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)

def swipLeft(driver, t=500, n=1):
    '''向左滑动屏幕'''
  l = driver.get_window_size()
    x1 = l['width'] * 0.75
  y1 = l['height'] * 0.5
  x2 = l['width'] * 0.25
  for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)

def swipRight(driver, t=500, n=1):
    '''向右滑动屏幕'''
  l = driver.get_window_size()
    x1 = l['width'] * 0.25
  y1 = l['height'] * 0.5
  x2 = l['width'] * 0.75
  for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)

# 选择新闻
def selectNews(driver):
    news = []
    try:
        news = driver.find_element_by_id("com.jifen.qukan:id/kt").find_elements_by_xpath(
            "//*[@resource-id='com.jifen.qukan:id/uo']")
    except BaseException as e:
        swipeUp(driver,2000,1)
        news = selectNews(driver)
    return news
# 阅读新闻
def readNew(driver):
    news = selectNews(driver)
    for index,item1 in enumerate(news):
        print(item1.text)
        item1.click()
        # 等待页面加载
  sleep(3)
        # 检查是否是广告
  try:
            gbBtn = driver.find_elements_by_xpath("//android.widget.TextView[@text='关闭']")
            if (gbBtn):
                driver.keyevent(4)
        except BaseException as e:
            print(e)
        swipeUp(driver,6000,7)
        swipeDown(driver, 6000, 7)
        sleep(3)
        driver.keyevent(4)
        sleep(3)

# main
header = driver.find_element_by_id("com.jifen.qukan:id/qk").find_element_by_class_name("android.widget.LinearLayout")

list = header.find_elements_by_class_name("android.widget.TextView")

# 找到标题元素 循环点击
for index,item in enumerate(list):
    print(item.text)
    item = list[index+4]
    if(index==6):
        # 如果到了第六个滑动一下
  swipLeft(item,3)
    item.click()
    # 然后开始查找新闻列表
 # 阅读新闻函数 # 每个分类阅读的页数  pageNum = 0
  while(pageNum<10):
        readNew(driver)
        swipeUp(driver,2000,1)
        pageNum+=1
print(list)
driver.quit()

以上脚本运行就可以让新闻自动阅读啦。
记录备忘

我的个人博客

上一篇下一篇

猜你喜欢

热点阅读