python-appium自動化操作微信

1.準備工作

java-jdk安裝(appium啓動必須要求安裝Java)

appium安裝

adb安裝

appium 模塊Appium-Python-Client安裝

2.各個應用安裝說明

java-jdk

這個安裝,需要注意環境變量的配置

配置jdk,新建環境變量(系統變量) JAVA_HOME,如圖所示:

 

系統變量->找到 Path 變量->編輯->在變量值的末尾添加;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin

 

如果在你下載的文件裏面找不到jre/bin的文件,即下面這個目錄,一定要以管理員模式打開cmd,進入java的jdk安裝目錄,輸入

bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre  回車即可。

 

 檢驗方式

輸入java -version,查看版本。

 

參考鏈接:https://zhuanlan.zhihu.com/p/360519888

 

Appium

是一個開源工具,用於自動化 iOS 手機、 Android 手機和 Windows 桌面平臺上的原生、移動 Web 和混合應用。連接手機,啓動安裝後界面如下

下載鏈接:https://appium.io/

 

adb安裝

這個目的是爲了下發手機相關命令,下載後注意手動去配置環境變量。

下載鏈接:https://developer.android.google.cn/studio?hl=zh-cn

 

Appium-Python-Client

這個強烈建議不要下載最新版本,後續自動化測試,會導致和appium某些功能不適配,建議下載Appium-Python-Client版本在1.3.0或1.3.0以下。

(過程中可能會出現Method has not yet been implemented的報錯,此錯誤是因爲appium sever的版本和Appium-Python-Client  的版本不一致)

下載鏈接:http://appium.io/downloads.html

 

2.appium連接手機配置

我的p40pro手機,配置如下:

 

啓動後效果:

 

 

 python啓動,基本參數配置如下

from appium import webdriver
from selenium.webdriver.common.by import By

desired_caps = {
    "platformName": "Android",  # 操作系統
    "deviceName": "UQG5T20610006585",  # 設備 ID adb devices
    "platformVersion": "10",  # 設備版本號
    "appPackage": "com.tencent.mm",  # app 包名
    "appActivity": "com.tencent.mm.ui.LauncherUI",  # app 啓動時主 Activity
    'noReset': True,  # 是否保留 session 信息,可以避免重新登錄
    'autoLaunch Appium': True,
    'automationName': 'UiAutomator1',
    'unicodeKeyboard': True,  # 使用 unicodeKeyboard 的編碼方式來發送字符串
    'resetKeyboard': False  # 將鍵盤給隱藏起來
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
print("微信啓動")

這裏需要注意其中的automationName這兩個參數,如果這個參數報錯,那麼可能是UiAutomator2,以報錯說明爲準

 

以下是參考網上,微信對刪除自己的人的清除操作的代碼,有一下幾個注意點:

1. find_element_by_id方法的廢棄,現在統一爲了find_element

2.手機上滑,座標系的取值,即driver.swipe(x1, y1, x1, y2, duration=500)方法,y2與y1之間儘量差距小一點,滑動點小一點,這樣獲取微信好友不容易漏掉

3.代碼中所有的定位元素id參數修改,如driver.find_elements(By.ID, 'com.tencent.mm:id/hga')中的com.tencent.mm:id/hga。

微信版本變化,它的id值可能都會變化。我拿過來這段代碼,當中所有的定位元素基本都做了修改

4.微信好友名稱的特殊化處理,有的微信名稱裏面,有花花草草貓貓狗狗,特殊表情符號這些。雖然都能獲取到,但是逐個取值,搜索欄搜索定位過程可能導致搜索不到最終報錯

以下是代碼,對下面代碼基本喫透,appium算是上手了

import time
from appium import webdriver
from selenium.webdriver.common.by import By


# 判斷元素是否存在
def is_element_exist(element, timeout=3):
    count = 0
    while count < timeout:
        souce = driver.page_source
        if element in souce:
            return True
        else:
            count += 1
            time.sleep(1)
    return False


desired_caps = {
    "platformName": "Android",  # 操作系統
    "deviceName": "UQG5T20610006585",  # 設備 ID adb devices
    "platformVersion": "10",  # 設備版本號
    "appPackage": "com.tencent.mm",  # app 包名
    "appActivity": "com.tencent.mm.ui.LauncherUI",  # app 啓動時主 Activity
    'noReset': True,  # 是否保留 session 信息,可以避免重新登錄
    'autoLaunch Appium': True,
    'automationName': 'UiAutomator1',
    'unicodeKeyboard': True,  # 使用 unicodeKeyboard 的編碼方式來發送字符串
    'resetKeyboard': False  # 將鍵盤給隱藏起來
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
print("微信啓動")


# 上拉方法
def swipe_up(distance):  # distance爲滑動距離,time爲滑動時間
    size = driver.get_window_size()
    # print(size)
    x1 = 0.5 * size['width']
    y1 = 0.8 * size['height']
    y2 = (0.8 - distance) * size['height']
    driver.swipe(x1, y1, x1, y2, duration=500)


    # 獲取通訊錄列表
def get_address_list(flag):
    if flag == True:
        driver.find_elements(By.ID, 'com.tencent.mm:id/f2i')[1].click()
        # 上滑  時間越短,劃得屏幕越大
        swipe_up(0.2)
    else:
        swipe_up(0.21)
    # 獲取暱稱(備註)
    address_list = driver.find_elements(By.ID, 'com.tencent.mm:id/hga')
    remarks = []
    for address in address_list:
        remark = address.get_attribute("text")
        # 白名單
        if remark != '個人微信名字' and ("微信團隊" not in remark) and ("文件傳輸助手" not in remark):
            remarks.append(remark)
    return remarks


# 判斷是否被刪
def is_delete(remark, count):
    if count == "1":
        time.sleep(0.2)
        print('點擊微信搜索框')
        driver.find_element(By.ID, 'com.tencent.mm:id/gss').click()
    time.sleep(0.1)
    print('在搜索框輸入搜索信息')
    driver.find_element(By.ID, 'com.tencent.mm:id/cd6').send_keys(remark)
    time.sleep(0.1)
    print('點擊搜索到的好友')
    driver.find_element(By.ID, 'com.tencent.mm:id/kpx').click()
    time.sleep(0.1)

    print('功能按鈕展開')
    driver.find_element(By.ID, 'com.tencent.mm:id/b3q').click()
    time.sleep(0.1)

    print('點擊轉賬按鈕')
    driver.find_elements(By.ID, 'com.tencent.mm:id/ve')[5].click()
    time.sleep(0.1)

    print('跳轉,開始轉錢')
    driver.find_element(By.ID, 'com.tencent.mm:id/ffn').click()
    time.sleep(0.1)
    print('確認')
    driver.find_element(By.ID, 'com.tencent.mm:id/ffw').click()
    time.sleep(0.1)
    # 判斷是否被刪
    is_exist = is_element_exist('com.tencent.mm:id/guv')
    if is_exist is True:
        return remark
    else:
        return False


# 返回搜索框
def search_back():
    time.sleep(0.1)
    driver.find_element(By.ID, 'com.tencent.mm:id/fz').click()
    time.sleep(0.1)
    driver.find_element(By.ID, 'com.tencent.mm:id/fz').click()
    time.sleep(0.1)
    # 清除搜索框,輸入下一個
    driver.find_element(By.ID, 'com.tencent.mm:id/k1h').click()


# 刪除把自己刪除的人
def del_person(nicks):
    print('點擊微信搜索框')
    driver.find_element(By.ID, 'com.tencent.mm:id/gss').click()
    for inx, val in enumerate(nicks):
        time.sleep(0.5)
        if inx == 0:
            print('在搜索框輸入搜索信息')
            driver.find_element(By.ID, 'com.tencent.mm:id/cd6').send_keys(val)
        else:
            time.sleep(1)
            print('點擊微信搜索框')
            driver.find_element(By.ID, 'com.tencent.mm:id/gss').click()
            print('在搜索框輸入搜索信息')
            time.sleep(0.5)
            driver.find_element(By.ID, 'com.tencent.mm:id/cd6').send_keys(val)
        time.sleep(0.5)
        print('點擊搜索到的人')
        driver.find_element(By.ID, 'com.tencent.mm:id/kpx').click()
        time.sleep(0.5)
        print('點擊聊天對話框右上角...')
        driver.find_element(By.ID, 'com.tencent.mm:id/en').click()
        time.sleep(1)
        print('點擊頭像')
        driver.find_element(By.ID, 'com.tencent.mm:id/iwj').click()
        time.sleep(1)
        print('點擊聯繫人右上角...')
        driver.find_element(By.ID, 'com.tencent.mm:id/en').click()
        time.sleep(1)
        print('點擊刪除按鈕')
        driver.find_element(By.ID, 'com.tencent.mm:id/khu').click()
        time.sleep(1)
        print('點擊彈出框中的刪除')
        driver.find_element(By.ID, 'com.tencent.mm:id/gv3').click()


if __name__ == '__main__':
    remarks = []
    driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
    # 打開微信需要時間
    time.sleep(5)
    remarks1 = get_address_list(True)
    remarks.extend(remarks1)
    while True:
        # 是否到了通訊錄末尾
        is_end = is_element_exist('com.tencent.mm:id/bml')
        # time.sleep(0.2)
        remarks2 = get_address_list(False)
        remarks.extend(remarks2)
        if is_end == True:
            break
    remarks = set(remarks)
    print("通訊錄暱稱列表:", remarks)
    print("好友數目:", len(remarks))
    time.sleep(1)
    dels = []
    for inx, val in enumerate(remarks):
        rt = ""
        if inx == 0:
            rt = is_delete(val, "1")
        else:
            rt = is_delete(val, "")
        if rt is False:
            # 返回搜索框
            driver.keyevent(4)
            search_back()
        # 被刪除
        else:
            dels.append(rt)
            time.sleep(0.1)
            driver.find_element(By.ID, 'com.tencent.mm:id/gv3').click()
            search_back()
    print("刪除我的人:", dels)
    # 刪除刪了自己的人
    del_person(dels)

 

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