在测试过程中发现部分APP启动过程中会出现广告业、引导页。大部分都是图片,部分是页面。无法点击跳过或开始。
只能借助图片识别的能力,决定使用第三方的图片识别,返回文字及坐标的功能。如下:(每天500,完全够用)
- 登录获取Token:
def getToken():
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&" \
"client_id={}&client_secret={}".format(client_id, client_secret)
response = requests.get(host)
if response:
return response.json()['access_token']
- 调用识别接口:
def findText(access_token, imgDir):
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"
# 二进制方式打开图片文件
f = open(imgDir, 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
return response.json()
- 识别按钮:
MSGS = ["确定", "允许", "同意", "跳过", "我同意", "立即使用", "下一步", "开启", "是", '跳过广告']
def get_location(words_result):
k = True
for word in words_result:
if str(word['words'].strip(string.digits)) in MSGS:
top = int(word['location']['top']) + int(word['location']['height'] / 2)
left = int(word['location']['left']) + int(word['location']['width'] / 2)
return top, left
else:
k = False
return k
- 滑动点击:
def dragPage(self):
size = self.d.window_size()
w = size[0]
h = size[1] / 2
for i in range(6):
self.d.drag(w - 1, h, 1, h)
tm = "./resTest/screenshot/{}.png".format(str(time.time()))
self.d.screenshot(tm)
dian = get_location(findText(getToken(), tm))
if dian:
self.d.click(dian[0], dian[1])
else:
self.d.press('back')