1、思路
根据appium连接安卓真机进行一系列操作
2、代码
# -*- coding: UTF-8 -*-
# v0.1-20201125
from appium import webdriver
import time
appdata = {
'noReset': True,
'platformVersion': '10',# (Android)系统版本号
'appPackage': 'com.eg.android.AlipayGphone',# APP包名
'appActivity': 'com.eg.android.AlipayGphone.AlipayLogin',# APP包启动路径
'platforVersion': '4.4.2',
'uuid': '',
'deviceName': 'test_phone',# 别名
'platformName': 'Android'# 设备型号
}
shenlindata = {
'entrance_identification': '蚂蚁森林',# 首页的‘蚂蚁森林’文字标识
'energy_map_id': 'J_tree_dialog_wrap',# ‘找能量’ID
'energy_map_coords': [(971,1558)],#[(540,612),(1080,1623)],# ‘找能量’坐标
'energy_pay_text': '我的大树养成记录',# ‘找能量’后页面的标识
'steal_coords': [[(216,750)],[(298,701)],[(381,653)],[(463,605)],[(546,605)],[(628,653)],[(711,701)],[(793,750)]] # “偷”能量的点击坐标列表值(有待于优化)
}
timesleepdata = {
'app_start': 0 # APP启动后等待时间
}
appiumurl = 'http://127.0.0.1:4723/wd/hub'
# 点击'id'
def click_id( driver, id ):
try:
driver.find_element_by_id(id).click()
return True
except Exception as args:
print(args)
# 点击'accessibility id'
def click_accessibility_id( driver, mId ):
try:
#click_linkmanMsgidCoords(driver, mId)
driver.find_element_by_accessibility_id(mId).click()
return True
except Exception as args:
print(args)
# 点击某个‘mId’元素第一次出现是的坐标
def click_linkmanMsgidCoords( driver , mId):
try:
pageStr = driver.page_source
for line in pageStr.splitlines():
if mId in line:
line_subStr = line.split()
#print(line_subStr)
for s in line_subStr:
if 'bounds=' in s:
s2 = s.replace('bounds=','|').replace('"','').replace('[','|').replace(']','|').replace(',','|')
s3 = s2.split('|')
for ss3 in s3:
if '' is ss3:
s3.remove(ss3)
c1 = (int(s3[0]),int(s3[1]))
c2 = (int(s3[2]),int(s3[3]))
coords = [c1,c2]
return click_tap(driver, coords)
except Exception as args:
print(args)
# 点击'坐标点'
def click_tap( driver, coords ):
try:
driver.tap(coords)
return True
except Exception as args:
print(args)
# 判断’page‘中是否存在'sub_str'
def judge_driver_element( sub_str, str ):
try:
if str is None:
return False
if sub_str in str:
return True
except Exception as args:
print(args)
# 日志输出至文件
def log( logstr ):
f = open('log.txt', 'w')
print(logstr.encode('utf-8'), file = f)
f.close()
# 执行‘死循环’的【偷】当前页面
def steal_run_while( driver ):
for steal_c in shenlindata['steal_coords']:
try:
click_tap(driver, steal_c)
time.sleep(1)
except Exception as args:
print(args)
# 已经进入蚂蚁森林中,准备‘死循环’的【偷】所有可‘偷’的人
def sl_run( driver ):
while True:
try:
flag = judge_driver_element(shenlindata['energy_map_id'], driver.page_source)
if(flag):
flag = click_id(driver, shenlindata['energy_map_id'])
time.sleep(1)
print('---1')
print(flag)
if(flag):
steal_run_while(driver)
else:
flag = click_tap(driver, shenlindata['energy_map_coords'])
time.sleep(1)
print('----2')
print(flag)
if(flag):
steal_run_while(driver)
else:
flag = click_tap(driver, shenlindata['energy_map_coords'])
time.sleep(1)
print('---3')
print(flag)
if(flag):
steal_run_while(driver)
except Exception as args:
print(args)
break
def run_phone( appiumurl, appdata):
driver = None
try:
driver = webdriver.Remote(appiumurl, appdata)# 打开手机APP
timesleep = timesleepdata['app_start']
if timesleep > 0:
time.sleep(timesleep)# 等待'APP'启动
if driver is None:
print('APP启动失败')
else:
flag = judge_driver_element(shenlindata['entrance_identification'], driver.page_source)
if(flag):
flag = click_linkmanMsgidCoords(driver, shenlindata['entrance_identification'])
print(flag)
sl_run(driver)
except Exception as args:
print(args)
def main():
print('APP启动')
while True:
try:
run_phone(appiumurl, appdata)
except Exception as args:
print(args)
print('运行结束')
if __name__ == '__main__':
main()
3、需要修改和优化地方
appdata.uuid:根据自己使用安卓真机填写(一般可使用“adb devices”获取)
appdata.platformVersion:(Android)系统版本号根据安卓真机填写(不用填写系统小版本号,只要大版本号,例如:10、9、8、7...)
shenlindata.steal_coords:“偷”能量的点击坐标列表值,一个弧形的路线(一般会跟手机默认的分辨率有关,也跟“树”的装扮有关)
shenlindata.energy_map_coords:‘找能量’坐标位置,一般会跟手机默认的分辨率有关(使用AppiumUI/代码断点调试自行获取即可)
4、附言
本代码将持续更新,
同时也欢迎各路大神指导;
如有新的思路等办法,
也欢迎指导、讨论...