1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        使用 Pywinauto自動發(fā)送微信消息

        共 4277字,需瀏覽 9分鐘

         ·

        2021-08-29 02:32




        作者 | 星安果
        來源 | AirPython

        1. 前言

        之前介紹過如何使用 PyWinAuto爬取朋友圈,今天為大家繼續(xù)介紹如何使用PyWinAuto發(fā)微信消息!

        2. 介紹

        Pywinauto,完全由 Python 構(gòu)建的一個模塊,可以用于自動化 Windows 上的 GUI 應(yīng)用程序

        同時,它支持鼠標(biāo)、鍵盤操作,在元素控件樹較復(fù)雜的界面,可以輔助我們完成自動化操作

        項目地址:https://github.com/pywinauto/pywinauto

        支持的應(yīng)用為下面 2 大類:

        • Win32 API  

          包含 MFC、VB6、VCL、simple WinForms controls and most of the old legacy apps

        • MS UI Automation

          包含 WinForms、WPF、Store apps、Qt5, browsers

        其中

        win32 API 的 backend 為「 win32

        MS UI Automation 的 backend 為「 uia

        3. 元素控件

        和 WinAppDriver 類似,在編寫自動化腳本之前,我們需要先獲取元素控件的各類屬性值

        獲取元素控件有 2 種方式:

        • 窗口內(nèi)置方法

        • 檢查工具

        其中

        窗口對象內(nèi)置的 2 個方法都可以打印出當(dāng)前頁面的元素控件樹

        # 窗口對象中內(nèi)置方法,打印窗體內(nèi)所有元素控件
        # 方式一:
        窗體對象.print_control_identifiers()

        # 方式二
        窗體對象.dump_tree()

        常用的檢查工具也包含 2 種,分別是:Inspect.exe、Spy++

        4. 實(shí)戰(zhàn)一下

        在實(shí)戰(zhàn)之前,我們先安裝依賴包 pywinauto

        # 安裝依賴
        pip3 install pywinauto

        接下來,我們還是以上篇文章中的例子來講解完整的實(shí)現(xiàn)過程

        4-1  打開微信客戶端

        首先,通過檢查工具判斷應(yīng)用的 backend 屬性值,PC 端微信的 backend 為「 uia 」

        然后,實(shí)例化一個「 Application 」類

        最后,使用該對象內(nèi)置的函數(shù)創(chuàng)建一個應(yīng)用對象

        import pywinauto
        from pywinauto.application import Application

        # 獲取應(yīng)用對象
        # 三種方式任選一種
        # 方式一:應(yīng)用進(jìn)程pid(連接)
        app = Application(backend='uia').connect(process=pid)

        # 方式二:應(yīng)用完整路徑(連接)
        app = Application(backend='uia').connect(path="D:\Program Files (x86)\Tencent\WeChat\WeChat.exe")

        # 方式三:打開應(yīng)用(打開)
        app = Application(backend='uia').start('D:\Program Files (x86)\Tencent\WeChat\WeChat.exe')

        需要指出的是,獲取應(yīng)用對象有上面 3 種方式,可以按需使用

        4-2  獲取窗體對象

        通過檢查工具獲取窗體的屬性列表,然后使用應(yīng)用對象 + 窗體屬性獲取微信首頁的窗體對象

        實(shí)現(xiàn)代碼如下:

        from pywinauto.win32functions import SetFocus

        # 獲取窗口對象
        # 通過title及ClassName獲取窗體對象
        self.weixin_pc_window = self.app.window(title=u"微信", class_name="WeChatMainWndForPC")

        self.weixin_pc_window.set_focus()

        4-3  切換到聊天列

        獲取左側(cè)聊天切換按鈕,獲取其坐標(biāo)位置,模擬點(diǎn)擊進(jìn)入到聊天列表頁面

        from pywinauto import mouse

        def __get_element_postion(self, element):
            """獲取元素的中心點(diǎn)位置"""
            # 元素坐標(biāo)
            element_position = element.rectangle()
            # 算出中心點(diǎn)位置
            center_position = (int((element_position.left + element_position.right) / 2),
                                   int((element_position.top + element_position.bottom) / 2))
            return center_position

        def start(self):
            # 1、獲取左側(cè)【聊天】切換元素
            chat_list_element = self.weixin_pc_window.child_window(title="聊天", control_type="Button")
            # 2、點(diǎn)擊進(jìn)入到聊天列表
            mouse.click(button='left',
                            coords=self.__get_element_postion(chat_list_element))

        4-4  進(jìn)入聊天頁面,輸入內(nèi)容并發(fā)送

        獲取「 文件聊天助手 」元素,點(diǎn)擊進(jìn)入到聊天頁面,找到輸入框元素,使用內(nèi)置方法輸入內(nèi)容,最后使用鍵盤模擬點(diǎn)擊「 Enter 」鍵,發(fā)送消息

        from pywinauto import mouse

        # 3、點(diǎn)擊【文件傳輸助手】進(jìn)入到聊天頁面
        file_helper_element = self.weixin_pc_window.child_window(title="文件傳輸助手", control_type="ListItem")

        mouse.click(button='left',
                            coords=self.__get_element_postion(file_helper_element))
        # 4、獲取輸入框元素,模擬輸入
                edit_element = self.weixin_pc_window.child_window(title=r"輸入", control_type="Edit")

        sleep(2)
        # 輸入內(nèi)容
        edit_element.type_keys("星安果")
        # 使用鍵盤模擬回車,即:發(fā)送
        send_keys('{ENTER}')

        4-5  釋放資源

        完成操作后,可以調(diào)用應(yīng)用對象的「 kill() 」函數(shù)關(guān)閉進(jìn)程,釋放掉資源

        def teardown(self):
            """釋放資源"""
            # 結(jié)束進(jìn)程,釋放資源
            self.app.kill()

        5. 最后

        上例中使用 pywinauto 完成了一個簡單的發(fā)送消息的自動化步驟

        實(shí)際項目中,結(jié)合鼠標(biāo)、鍵盤可以實(shí)現(xiàn)很多復(fù)雜場景的自動化,對這部分有興趣的可以點(diǎn)擊文末的「 閱讀原文 」去了解

        pywinauto 和 WinAppDriver 都能很好的完成 PC 端的自動化,但是由于 WinAppDriver 支持 Appium,在腳本語法簡潔性方面可能更有優(yōu)勢

        文中的所有源碼:https://github.com/xingag/test_auto/blob/master/pc/Pywinauto%E5%AE%9E%E4%BE%8B.py

        ??分享、點(diǎn)贊、在看,給個三連擊唄!
        瀏覽 145
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            伊人大香蕉电影网 | 国产无遮挡18禁无码网站不卡 | 爽好大快要尿了 | 色天堂在线看 | 男女成人毛片免费视频 | 国产精品视频自拍 | 久久久久女人精品毛片九一韩国 | 999色综合 | 中国操逼视频 | 色999av色 |