618到了,淘宝和支付宝都有浏览任务,简单写个Python脚本自动做浏览任务,就不用了一个一个的点,代码主要通过APPium+Python写成,有可能有些xpath路径更新,获取不到节点,请自行测试。
代码:
# -*- coding: utf-8 -*-
"""
Created on Sat May 30 21:41:43 2020
@author: 14504
"""
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver . common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from appium.webdriver.common.touch_action import TouchAction
import time
import re
desired_caps={
"platformName": "Android",
"deviceName": "Redmi_K20",
"appPackage": "com.taobao.taobao",
"appActivity": "com.taobao.tao.TBMainActivity",
"skipServerInstallation": "True",
"skipDeviceInitialization": "True",
"noReset": "True"
}
server = 'http://localhost:4723/wd/hub'
driver = webdriver.Remote (server, desired_caps)
wait = WebDriverWait(driver, 20)
def TBtask(view):
try:
viewPath='//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View/android.view.View/android.view.View[1]/android.widget.ListView/android.view.View[{}]/android.widget.Button'.format(view)
messagePath='//com.uc.webview.export.WebView[@content-desc="WVUCWebView"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[7]/android.view.View/android.view.View/android.view.View[1]/android.widget.ListView/android.view.View[{}]/android.view.View'.format(view)
message=wait.until(EC.presence_of_element_located((By.XPATH,messagePath)))
text=re.search('\((.*?)/(.*?)\)', message.text)
first=int(text.group(1))
two=int(text.group(2))
while(first < two) :
print('task{}需要浏览{}次,正在浏览第{}次......'.format(view,two,first+1))
task=wait.until(EC.presence_of_element_located((By.XPATH,viewPath)))
task.click()
first_test=first
time.sleep(27)
print('15s浏览结束,正在返回')
driver.keyevent(4)
time.sleep(2)
message=wait.until(EC.presence_of_element_located((By.XPATH,messagePath)))
text=re.search('\((.*?)/(.*?)\)', message.text)
first=int(text.group(1))
two=int(text.group(2))
if(first==first_test):
print('此任务不是浏览任务,进行下一个任务')
break
print('task{}浏览任务完成'.format(view))
except TimeoutException:
print('task{}请求超时,进行下一个任务'.format(view))
def TBmiaobi():
print('淘宝浏览任务开始.......')
try:
lieche = wait.until(EC.presence_of_element_located((By.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.support.v7.widget.RecyclerView/android.widget.FrameLayout[6]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[4]")))
lieche.click()
time.sleep(7)
TouchAction(driver).tap(x=508, y=1541).perform()
getMiaoBi= wait.until(EC.presence_of_element_located((By.XPATH,'//com.uc.webview.export.WebView[@content-desc=\"WVUCWebView\"]/com.uc.webkit.ay/android.webkit.WebView/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[3]/android.view.View/android.view.View[4]/android.widget.Button')))
getMiaoBi.click()
for view in range(2,7):
if(view != 4):
TBtask(view)
print('淘宝所有浏览任务已基本完成,还有没完成的,请重新开始')
except TimeoutException :
print('淘宝浏览任务超时,程序结束.......')
def ZFBtask():
i=1
while(i<=5):
print('浏览第{}次.....'.format(i))
try:
el1 =wait.until(EC.presence_of_element_located((By.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.az/android.webkit.WebView/android.view.View/android.view.View[3]/android.view.View/android.app.Dialog/android.view.View[2]/android.view.View[4]/android.view.View[2]/android.view.View/android.widget.Button")))
#el1 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.az/android.webkit.WebView/android.view.View/android.view.View[3]/android.view.View/android.app.Dialog/android.view.View[2]/android.view.View[5]/android.view.View[2]/android.view.View/android.widget.Button")
el1.click()
time.sleep(5)
driver.keyevent(4)
el2 =wait.until(EC.presence_of_element_located((By.XPATH,"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.az/android.webkit.WebView/android.view.View/android.view.View[1]/android.app.Dialog/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button[1]")))
#el2 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.az/android.webkit.WebView/android.view.View/android.view.View[1]/android.app.Dialog/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.Button[1]")
el2.click()
i=i+1
except TimeoutException:
print('请求超时,任务可能已完成,退出程序')
break
print('支付宝浏览任务完成')
def ZFBmiaobi():
print('支付浏览任务开始.......')
try:
driver.start_activity('com.eg.android.AlipayGphone','.AlipayLogin')
time.sleep(4)
TouchAction(driver).tap(x=525, y=661).perform()
time.sleep(5)
TouchAction(driver).tap(x=646, y=1488).perform()
getMiaoBi= wait.until(EC.presence_of_element_located((By.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/com.uc.webview.export.WebView/com.uc.webkit.az/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[4]/android.widget.Button')))
getMiaoBi.click()
except TimeoutException:
print('请求超时,退出程序')
else:
ZFBtask()
def main():
TBmiaobi()
print('\n')
ZFBmiaobi()
if __name__=='__main__':
main()