appnium适应之配置
阅读原文时间:2023年07月09日阅读:5

#获取包名和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=

  

  

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': '', # 超时时间
# '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()

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章