以操作小米商城下单为例
流程是 启动小米商城app, 点击分类,点击小米手机, 点击小米10 至尊版,点击加入购物车,点击确定....
原脚本
from time import sleep
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
caps = {
'platformName': 'Android',
'platformVersion': '6.0.1',
'deviceName': 'emulator-5554',
'appPackage': 'com.xiaomi.shop',
'appActivity': '.activity.MainTabActivity',
'automationName': 'uiautomator2'
}
dr = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
dr.implicitly_wait(10)
sleep(2)
# 处理弹出广告
try:
dr.find_element('xpath', '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ImageView')
dr.keyevent(4)
except:
print('未出现')
dr.find_element_by_xpath('//*[@text="分类"]').click()
dr.find_element_by_xpath('//*[@text="小米手机"]').click()
dr.find_element_by_xpath('//*[@text="小米10 至尊版"]').click()
dr.find_element_by_xpath('//*[@text="加入购物车"]').click()
dr.find_element_by_xpath('//*[@text="确定"]').click()
操作封装
import subprocess
from time import sleep
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
APPIUM_SERVER = 'http://127.0.0.1:4723/wd/hub'
AUTOMATION_NAME = 'uiautomator2'
def run_cmd(cmd):
p1 = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
output = p1.stdout.read().strip().decode('utf-8')
return output
class Device:
def __init__(self, device_name=None, version=None, platform='Android'):
self.driver = None
self.device_name = device_name or self.get_device_name()
self.version = version or self.def get_platform_version()
print(f'初始化设备 {platform} {version} {device_name}')
self.caps = dict(platformName=platform, platformVersion=version,
deviceName=device_name, automationName=AUTOMATION_NAME)
@staticmethod
def get_device_name():
deivce_name = run_cmd('adb get-serialno')
return device_name
@staticmethod
def get_platform_version()
platform_version = run_cmd('adb shell getprop ro.build.version.release')
return platform_version
def launch_app(self, package, activity):
print(f'启动应用 {package}/{activity}')
self.caps.update(appPackage=package, appActivity=activity)
self.driver = webdriver.Remote(APPIUM_SERVER, self.caps)
self.driver.implicitly_wait(10)
return self
def find(self, target):
# target 为 id=kw 格式, 当不包含=时,默认为text=target
by, value = target.split('=') if '=' in target else 'text', target
if by == 'text':
by, value = 'xpath', f'//*[@text="{value}"]'
return self.driver.find_element(by, value)
def try_find(self, target):
try:
return self.find(target)
except NoSuchElementException:
print(f'元素 {target} 未出现')
def click(self, target):
print(f"点击 {target}")
self.find(target).click()
return self # 返回self可使得对象支持链式操作
def input_to(self, target, text):
print(f"在 {target} 输入 {text}")
self.find(target).send_keys(text)
return self
def back(self):
print('返回')
self.driver.keyevent(4)
return self
def wait(self, secs=1):
sleep(secs)
return self
使用
mumu = Device() # 自动获取当前第一个设备及平台版本
mumu.launch_app(package='com.xiaomi.shop', activity='.activity.MainTabActivity').wait(2)
# 处理弹出广告
ads = mumu.try_find('xpath=/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/'
'android.widget.FrameLayout/android.widget.ImageView')
mumu.back()
# 加购操作
mumu.click('分类').click('小米手机').click('小米10 至尊版').click('加入购物车').click('确定')
运行输出如下
初始化设备 Android 6.0.1 emulator-5554
启动应用 com.xiaomi.shop/com.xiaomi.shop2.activity.MainActivity
返回
点击 分类
点击 小米手机
点击 小米10 至尊版
点击 加入购物车
点击 确定