1. Introduction

Hello, I’m Anguo!

In my last article, I talked about a PC side automation solution: WinAppDriver

Talk about the best PC automation scheme – WinAppDriver

Some friends left me a message in the background, saying that “PyWinAuto”, as an alternative to WinAppDriver, can also complete the PC automation perfectly

2. Introduction

Pywinauto, a module built entirely from Python, can be used to automate GUI applications on Windows

At the same time, it supports mouse, keyboard operation, in the element control tree more complex interface, can assist us to complete automatic operation

Project address: github.com/pywinauto/p…

The supported applications fall into the following two categories:

  • Win32 API  

    Includes MFC, VB6, VCL, Simple WinForms Controls and most of the old Legacy Apps

  • MS UI Automation

    Includes WinForms, WPF, Store Apps, Qt5, Browsers

Among them

The backend of win32 API is “Win32”.

Backend for MS UI Automation is “UIA”

3. Element controls

Like WinAppDriver, we need to get the various property values of the element control before writing the automation script

There are two ways to get element controls:

  • Built-in window methods

  • Check tools

Among them

The window object has two built-in methods that print out a tree of element controls for the current page

Print_control_identifiers () # window object built-in method that prints all element controls in the formCopy the code

Common inspection tools also include two kinds, respectively: inspect.exe, Spy++

4. Get real

Before going into action, install the dependency package PyWinAuto

Pip3 install PyWinAutoCopy the code

Next, let’s use the examples from the previous article to illustrate the full implementation process

4-1 Open the wechat client

First, check the backend value of the application. The backend value of wechat on the PC is uIA.

Then, instantiate an “Application” class

Finally, an application object is created using the built-in functions of the object

Import PyWinAuto from PyWinAuto. Application import Application # Pid (connection) app = Application(backend='uia'). Connect (process=pid) App = Application(backend='uia').connect(path="D: Program Files (x86)\Tencent\WeChat\WeChat. Exe ") App = Application(backend='uia'). Start ('D: Program Files (x86)\Tencent\WeChat\WeChat. Exe ')Copy the code

It should be noted that there are three ways to obtain application objects, which can be used on demand

4-2 Gets the form object

Get the form’s property list through the inspection tool, and then use the Application object + Form property to get the form object of wechat home page

The implementation code is as follows:

From pywinAuto. Win32functions import SetFocus # Retrieve window object self.weixin_pc_window = from pywinAuto. Win32functions import SetFocus # retrieve window object self.weixin_pc_window = Self.app.window (title=u"微信", class_name="WeChatMainWndForPC") self.weixin_pc_window.set_focus() self.app.window() self.weixin_pc_window.set_focus()Copy the code

4-3 Switch to chat list

Get the chat switch button on the left, get its coordinate position, and simulate clicking to enter the chat list page

from pywinauto import mouse def __get_element_postion(self, element): Rectangle () # rectangle = element_position (int((element_position.left +)  element_position.right) / 2), int((element_position.top + element_position.bottom) / 2)) return center_position def start(self): Chat_list_element = self. Weixin_pc_window. Child_window (title=" chat ", Click (Button ='left', coords=self.__get_element_postion(chat_list_element))Copy the code

4-4 Go to the chat page, enter the content, and send the message

Get the “file chat assistant” element, click to Enter the chat page, find the input box element, use the built-in method to Enter the content, finally use the keyboard simulation click “Enter” key, send a message

From PywinAuto import mouse # 3, click "File Transfer Assistant" to go to the chat page file_helper_element = Self. weixin_pc_window.child_window(title=" file transfer assistant ", control_type="ListItem") mouse. Click (button='left', Coords =self.__get_element_postion(file_helper_element) Edit_element = self.weixin_pc_window.child_window(title=r" enter ", Control_type ="Edit") sleep(2)Copy the code

4 to 5 Release resources

When the operation is complete, you can call the “kill()” function of the application object to shut down the process and release the resources

Def teardown(self): self.app.kill() def teardown(self): """Copy the code

5. The last

The above example uses PyWinAuto to complete a simple automated step of sending a message

In actual projects, the automation of many complex scenes can be realized by combining mouse and keyboard. If you are interested in this part, you can click “Read the original text” at the end of the article to learn about it

Pywinauto and WinAppDriver both do a good job of automating on the PC side, but WinAppDriver may have an advantage in terms of script syntax brevity because it supports Appium

All the source code in the article I have been uploaded to the public account background, follow the public account “AirPython”, reply keyword “winauto” to obtain the complete source code

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!