appnium适应之配置

一.session

 

#获取包名和acctivename
#这个工具在adk包里面
aapt.exe dump badging E:\Wandoujia_851097_web_seo_baidu_binded.apk

aapt.exe dump badging E:\Wandoujia_851097_web_seo_baidu_binded.apk |find “launchable-activity”

 

 

 寻找包名和activityname的另外一职中方式

C:\Windows\System32>adb shell
root@SM-G9350:/ # logcat |grep cmp=

  

  

 

2.python脚本驱动

from appium import  webdriver

cap={
  "platformName": "Android",
  "platformVersion": "5.1.1",
  "deviceName": "127.0.0.1:21533",
  "appPackage": "com.mei.wood",
  "appActivity": ".ui.SplashActivity",
  "noReset": True
}
driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",cap)

 淘宝列子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author tom

import time
from appium import webdriver
from appium.webdriver.common import mobileby
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


desired_capabilities = {
        'platformName': 'Android',                          # 系统
        'deviceName': '127.0.0.1:21533',                     # 移动设备号
        'platformVersion': '5.1.1',                         # 系统版本
        'appPackage': 'com.taobao.taobao',                  # 操作的app
        'appActivity': 'com.taobao.tao.welcome.Welcome',    # 打开淘宝app首页
        'unicodeKeyboard': True,
        'resetKeyboard': True,
        'dontStopAppOnReset': True,
        'autoGrantPermissions': True,
        'noReset': True,
        'automationName': 'uiautomator2',
        'newCommandTimeout': '36000',                       # 超时时间
        # 'systemPort': '8202',                               # 端口号,操作不用设备使用不同端口号
        # 'udid': 'xxxxxxxx',                                 # 移动设备号
        'command_executor': 'http://127.0.0.1:4723/wd/hub'  # 和启动命令保持一致
}


class AppiumDemo(object):
    def __init__(self):
        self.driver = webdriver.Remote(command_executor=desired_capabilities['command_executor'],
                                       desired_capabilities=desired_capabilities)
        self.by = mobileby.MobileBy()
        #点击同意使用流量
        # if self.driver.find_element_by_id("com.taobao.taobao:id/uik_mdButtonDefaultPositive"):
        #     self.driver.find_element_by_id("com.taobao.taobao:id/uik_mdButtonDefaultPositive").click()
        # 点击同意使用协议
        if self.driver.find_element_by_id("com.taobao.taobao:id/yes"):
            self.driver.find_element_by_id("com.taobao.taobao:id/yes").click()
        # # 点击更新
        # if self.driver.find_element_by_id("com.taobao.taobao:id/update_button_accept"):
        #     self.driver.find_element_by_xpath("com.taobao.taobao:id/update_button_accept").click()
        # 点击搜索框
        self.wait_find_element(by_type=self.by.ID, value='com.taobao.taobao:id/home_searchedit').click()
        # 点击店铺搜索
        self.wait_find_element(by_type=self.by.XPATH, value='//android.widget.TextView[@text="店铺"]').click()

    def wait_find_element(self, by_type: str, value: str, driver: WebDriver = None):
        """
        获取单个元素, 显式等待
        :param driver: 驱动对象
        :param by_type: 查找元素的操作
        :param value: 查找元素的方法
        :return:
        """
        driver = driver or self.driver
        if not driver:
            return driver
        try:
            WebDriverWait(driver, 10).until(EC.visibility_of_element_located(locator=(by_type, value)))
            return driver.find_element(by_type, value)
        except:
            # self.logger.warning(traceback.format_exc())
            return False

    def wait_find_elements(self, by_type: str, value: str, driver: WebDriver = None):
        """
        获取多个元素, 显式等待
        :param driver:
        :param by_type:
        :param value:
        :return:
        """
        driver = driver or self.driver
        if not driver:
            return driver
        try:
            WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(locator=(by_type, value)))
            return driver.find_elements(by_type, value)
        except:
            return False

    def get_size(self, driver: WebDriver = None):
        """
        获取屏幕大小
        :param driver:
        :return:
        """
        driver = driver or self.driver
        if not driver:
            return driver

        x = driver.get_window_size()['width']
        y = driver.get_window_size()['height']
        return [x, y]

    def swipe_up(self, driver: WebDriver = None, _time: int = 1000):
        """
        向上滑动
        :param driver:
        :param _time:
        :return:
        """
        driver = driver or self.driver
        if not driver:
            return driver
        try:
            size = self.get_size(driver)
            x1 = int(size[0] * 0.5)  # 起始x坐标
            y1 = int(size[1] * 0.80)  # 起始y坐标
            y2 = int(size[1] * 0.30)  # 终点y坐标
            driver.swipe(x1, y1, x1, y2, _time)
            return True
        except:
            return False

    def execute(self, seed):
        #清空搜索框,输入我们要爬取的数据
        self.wait_find_element(by_type=self.by.ID, value='com.taobao.taobao:id/searchEdit').clear().send_keys(seed['keyword'])
        #点击搜索按钮
        self.wait_find_element(by_type=self.by.ID, value='com.taobao.taobao:id/searchbtn').click()
        #获取店铺列表
        shop_list = self.wait_find_elements(by_type=self.by.ID, value='com.taobao.taobao:id/shopTitle')
        for shop_info in shop_list:
            shop_info.click()
            # 点击全部宝贝
            self.wait_find_element(by_type=self.by.XPATH, value='//android.widget.FrameLayout'
                                                                '[@content-desc="全部宝贝"]').click()

            for i in range(3):
                # 获取这一屏的数据
                item_list = self.wait_find_elements(by_type=self.by.ID, value="com.taobao.taobao:id/title")
                for item_info in item_list:
                    print(item_info.text)
                self.swipe_up()                 # 向上滑动
                time.sleep(0.5)                 # 一定要延时
            self.driver.back()                  # 返回上一级
        self.driver.back()


def main():
    seed = {
        'keyword': 'Python 书'
    }
    spider = AppiumDemo()
    while True:
        spider.execute(seed=seed)


if __name__ == '__main__':
    main()

 

posted @ 2019-05-09 16:29  阿布_alone  阅读(699)  评论(0编辑  收藏  举报
TOP