【Appium】Python+Appium實現支付寶螞蟻森林自動收取能量的一種解決方案

一、環境準備

首先,你需要一個能夠運行代碼的環境,這裏包括:

Node.js:安裝並配置環境變量

JAVA:安裝並配置環境變量

Android:安裝並配置環境變量

Python:安裝並配置環境變量

Appium-Windows-Desktop:安裝

Appium-Python-Client:在Pycharm中安裝

安卓模擬器(Genymotion或MuMu都可以)或者一部安卓手機

二、實現原理

這裏用到的知識主要包括:(都是一些比較簡單和基礎的知識,初學Appium的同學可以用來練習)

Appium對於元素以及屬性的定位

基本的點擊和滑動操作

關鍵元素特徵的判斷和識別

三、實現思路

1.利用appium獲取支付寶APP的包名以及入口的Activity,用來啓動APP

2.打開APP後進入螞蟻森林

3.首先收取自己的能量(這裏沒有做過多的判斷,直接把能量球可能出現的區域點擊一遍)

4.然後進入到好友列表,獲取好友列表中的所有好友

5.依次點擊當前頁面好友列表中的好友,進入好友的螞蟻森林,收取能量(這裏也沒有做過多的判斷,不管有沒有能量,遍歷所有好友)

6.當前頁面的好友全部收完之後,滑動到下一頁,繼續收取,直至好友列表遍歷結束。

四、實現代碼

from appium import webdriver
import time

desired_caps = {}

desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.1.1'
desired_caps['deviceName'] = '這裏填寫自己手機的設備名'
desired_caps['noReset'] = True
desired_caps['appPackage'] = 'com.eg.android.AlipayGphone'
desired_caps['appActivity'] = 'AlipayLogin'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(10)


def get_screen_size():
    x = driver.get_window_size()['width']  # 獲取屏幕寬度
    y = driver.get_window_size()['height']  # 獲取屏幕高度
    return (x, y)


# 在首頁找到【螞蟻森林】的入口,點擊進入
def get_home_view_by_text_then_click(driver,tag):
    home_app_view_list = driver.find_elements_by_id('com.alipay.android.phone.openplatform:id/home_app_view')
    home_len = len(home_app_view_list)
    for i in range(0,home_len):
        home_app_view_text = home_app_view_list[i].find_element_by_id('com.alipay.android.phone.openplatform:id/app_text').text
        print(home_app_view_text)
    for i in range(0,home_len):
        home_app_view_text = home_app_view_list[i].find_element_by_id('com.alipay.android.phone.openplatform:id/app_text').text
        if home_app_view_text == tag:
            print('-------------------------------------')
            print(str(tag) + '->' + str(i))
            print('點擊【' + home_app_view_text + '】')
            home_app_view_list[i].click()
            break


# 通用的查找指定元素並點擊的方法
def get_common_view_by_class_then_click(driver,tag):
    common_app_view_list = driver.find_elements_by_class_name('android.view.View')
    common_len = len(common_app_view_list)
    # for i in range(0,common_len):
    #     common_app_view_text = common_app_view_list[i].get_attribute('name')
    #     if common_app_view_text:
    #         print(common_app_view_text)
    for i in range(0,common_len):
        common_app_view_text = common_app_view_list[i].get_attribute('name')
        if common_app_view_text == tag:
            print('-------------------------------------')
            print(str(tag) + '->' + str(i))
            print('點擊【' + common_app_view_text + '】')
            common_app_view_list[i].click()
            break


# 實現一次按座標進行的點擊
def tap_alone(x,y,duration=1000):
    l = get_screen_size()
    width = int(l[0])  # 獲取屏幕寬度
    height = int(l[1])  # 獲取屏幕高度
    tap_x1 = int((int(x) / width) * width)
    tap_y1 = int((int(y) / height) * height)
    print('點擊座標:['+str(tap_x1)+', '+str(tap_y1)+']')
    driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], duration)


# 實現一次收取能量
def tap_get_energy():
    # 能量球可能出現的區域座標
    start_x = 120
    start_y = 470
    end_x = 920
    end_y = 870

    print('開始收取能量>>>')
    # 依次點擊指定區域內的等距離座標點
    for i in range(start_y,end_y,80):
        for j in range(start_x,end_x,80):
            print('['+str(j)+', '+str(i)+']',"\t", end="")
            tap_alone(j,i)
        print()


# 遍歷獲取好友列表
def get_friend_view_by_class_then_click(driver):
    time.sleep(1)
    for i in range(0,16):
        print(str(i))
        swipeUp(2000,500,3000)
    for i in range(0,16):
        print(str(i))
        swipeDown(500,2000,3000)
    time.sleep(1)

    swipeUp(2000, 1800)
    time.sleep(1)

    friend_view_square = driver.find_element_by_id('J_rank_list')
    friend_view_list = friend_view_square.find_elements_by_class_name('android.view.View')
    common_len = len(friend_view_list)
    print(common_len)
    print('-------------------------------------')
    count = 0
    for i in range(0,common_len):
        friend_view_view_text = friend_view_list[i].get_attribute('name')
        if friend_view_view_text:
            # print(friend_view_view_text)
            if count%10 == 0 and count != 0:
                count = 0
                print('向上劃一頁')
                swipeUp(2000, 120)
                time.sleep(1)
            # if '獲得了' in friend_view_view_text and count<10:
            if 'g' in friend_view_view_text:
                print('-------------------------------------')
                print('收取【'+friend_view_list[i-2].get_attribute('name')+'】的能量>>>')
                friend_view_list[i].click()
                count += 1
                # print(count)
                time.sleep(1)
                tap_get_energy()
                driver.press_keycode(4)

    # for i in range(0,common_len):
    #     friend_view_view_text = friend_view_list[i].get_attribute('name')
    #     if friend_view_view_text:
    #         print('-------------------------------------')
    #         print(str(friend_view_view_text) + '->' + str(i))
    #         print('點擊【' + friend_view_view_text + '】')
    #         friend_view_list[i].click()


# 實現安座標精準滑動:向上滑動
def swipeUp(y1,y2,duration=5000):
    l = get_screen_size()
    width = int(l[0])  # 獲取屏幕寬度
    height = int(l[1])  # 獲取屏幕高度
    x1 = int(width * 0.5)
    y1_start = int((int(y1) / height) * height)
    y2_end = int((int(y2) / height) * height)
    driver.swipe(x1, y1_start, x1, y2_end, duration)
    print('向上滑動【'+str(y1_start-y2_end)+'】')


# 實現安座標精準滑動:向下滑動
def swipeDown(y1,y2,duration=5000):
    l = get_screen_size()
    width = int(l[0])  # 獲取屏幕寬度
    height = int(l[1])  # 獲取屏幕高度
    x1 = int(width * 0.5)
    y1_start = int((int(y1) / height) * height)
    y2_end = int((int(y2) / height) * height)
    driver.swipe(x1, y1_start, x1, y2_end, duration)
    print('向下滑動【'+str(y2_end-y1_start)+'】')


# 點擊【螞蟻森林】
get_home_view_by_text_then_click(driver,'螞蟻森林')
time.sleep(5)

# 收取自己的能量
tap_get_energy()
time.sleep(3)

# 滑動屏幕,找到【好友排行榜】
swipeUp(2000, 100)
time.sleep(1)
swipeUp(2000,1100)
time.sleep(1)
# 點擊進入【好友排行榜】
get_common_view_by_class_then_click(driver,'查看更多好友')
# 開始收取好友列表中好友的能量
get_friend_view_by_class_then_click(driver)

time.sleep(5)
driver.quit()

五、運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章