[Turn] pywinauto Tutorial

First, the installation environment

1. command line installation method

pip install pywinauto==0.6.7

2. The method of manual installation

Installation package download link:
PyWin32: Python call windows api library
https://sourceforge.net/projects/pywin32/files/pywin32/Build 220 /

comtypes: COM dispatch interface https://github.com/enthought/comtypes/releases

six: compatibility library for Python2 and Python3 of
https://pypi.org/project/six/

Pillow: Optional, used to make screenshots
https://pypi.org/project/Pillow/2.7.0/

Pywinauto: PC-side automation tools
https://github.com/pywinauto/pywinauto/releases

Execute python setup.py install After unpacking
NOTE: It is recommended to use the first command-line installation and convenient.

3. Check the environment

Open the python command line interpreter, run the following code, windows notepad will be activated if there is no error, then prove pywinauto has been successfully installed.

from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")

Two, PC introduction and positioning tool end element determination backend

1.Backend judgment

Pywinauto in the backend, there are two: win32 and uia, the default is win32. Inspect and can be used spy ++ tools to determine which backend for writing. For example: If the UIA Inspect mode, controls and properties more visible then, UIA backend Alternatively, on the contrary, backend optional win32.


2. Introduction Tool

Spy ++ (tool positioning element (Win32))
the Inspect (tool positioning element (UIA))
the UI Spy (tool positioning element)
Swapy (pywinauto may simply generate code)

Third, start and create an instance of an object

1. Start

start () for the case has not started software. timeout timeout parameter (optional), if the time required to start a long software selectable timeout, the default timeout is 5s.
start (self, cmd_line, timeout = app_start_timeout)
Example:

app = Application(backend = ‘uia’).start(r"E:\Office\Office14\EXCEL.exe)

2. Connect

connect () for connecting the program has been started. Connected to an already running program in the following ways:

a) process: process id

app = Application().connect(process=2341)

Window handle of the application: b) handle

app = Application().connect(handle=0x010f0c)

c) path: a path for each execution path process (GetModuleFileNameEx module looks for the process and do it with us compare the incoming path)

app = Application().connect(path=“D:\Office14\EXCEL.exe”)

d) a combination of parameters (passed to pywinauto.findwindows.find_elements () function)

app = Application().connect(title_re=".*Notepad", class_name=“Notepad”)

Note: The
application must be ready to use connect (), the application start when there is no mechanism timeout and reconnection (after). And then start the application outside pywinauto, need sleep, and so on program start

Fourth, windows, dialog boxes, and control elements targeting methods

1.window, dialog positioning mode

1) is positioned on title

a) How to obtain the title?

title is the name of the window, you can use UISpy a kind of tool positioning element to find.
As illustrated, the dialog box for the Name property value title: "Replace"


b) If the tool can not find the title using the positioning element how to do?
Use print_control_identifiers () method to print out all the current window or dialog box title in
the format:

app.YourDialog. print_control_identifiers()

FIG example, demo see locate_by_title.py:


Written c) using the targeting methods of the title

Untitled_notepad = u 'None - Notepad' 
. App Untitled_notepad.draw_outline (Color = 'Red') # app.window (best_match = 'Untitled - Notepad')

Note: The wording applies to the English system, the software in English, other languages will exist coding problems, the need to transfer code reuse.
or

app [ 'None - Notepad'] .draw_outline (colour = 'red')

Note: For other systems in languages ​​other than English, without transcoding

2) top_window () Location

app.top_window () # This method returns the application of the top-most window (the window is not a window pop-up dialog box)
Note: This method has not been tested at present, it will return to top-level window of the application, but may not be Z- Order in the top-level window.

3) the keyword parameter passing
If the above methods do not meet the needs of the positioning element, the following list of parameters passed positioned reference elements, parameters can be used in combination.
Example:

app.window(class_name = ‘Notepad’).draw_outline(colour = ‘red’)

Common parameters:

Can pass parameters correspond to the attribute name Remarks
class_name ClassName
class_name_re regular matching window Classname
title the Name Window window name
title_re regular matching window name
best_match fuzzy matching a similar title
handle handle
framework_id FrameworkId (general FrameworkId not the only)
Process ProcessId, process id (note : after each start, process id will change)
CONTROL_ID CONTROL_ID
for CONTROL_TYPE with ControlType ()
auto_id the AutomationId

2. control positioning mode

Location-based title (with the window, title of the positioning dialog)

app[‘your dialog title’][‘your control title’]

or

app.dlg.control

Level positioning

app.window(class_name = ’Notepad’).window(class_name = ‘#32770’)
app.window(class_name = ‘Notepad’).child_window(class_name = ‘#32770’)

wpath Location
If the element is empty, or is not the only case, the positioning may be used in a manner similar to xpath selenium, the positioning element according to the element number check child.
Example:

app_window = app.window (class_name = 'Qt5QWindowIcon ') # login window positioned 
app_window.children () [1] .children ( ) [0] .children () [0] .children () [2] # locate the user name input box control (check numbers start with 0)

Fifth, a common method

1. debugging positioning control

a) print_control_identifiers (depth = None, filename = None)
to print out all tree control can be identified identification
depth: Depth print, the default maximum depth.
filename: returns the identifier saved as a file (file with the generated scripts currently running in the same path)
EG: DLG print_control_identifiers (filename = 'a.txt').

B) draw_outline (Color = 'Green', 2 = Thickness,
Fill = win32defines.BS_NULL, RECT = None)
defaults to draw a boundary around the current position of the window or control, is positioned to help us see which control
Color : the color of the boundary line, the default is green
thickness: line thickness, the default is 2
fill: the way in which a filled rectangle (not attempted, see source base_wrapper.py)
RECT: draw the coordinates of the rectangle (in the current default draw a rectangle) to the positioning element
c) is_dialog determines whether dialog

2. Implicit wait

A) the wait (wait_for, timeout = None, the retry_interval = None)
wait_for five kinds of parameters can be passed, it may be combined with parameter passing, but separated by a space:
EXISTS: window becomes active handle
visible: window is visible, no hidden
enabled : window does not disable
READY: + visible enable
Active: Active
timeout: set the timeout time until the window if no incoming wait_for in several states in the n seconds, is thrown TimeoutError.
retry_interval: After timeout interval n seconds retry again.
Dlg.wait ( "exists ready", timeout = 5, retry_interval = 3)

b) wait_not (wait_for_not, timeout = None, retry_interval = None)
waiting window is not in a certain state. Parameters and wait mass participation agreement.

3. input box

a)type_keys()
Dlg.control.type_keys(“xxxxx”)

4. Menu Bar

menu_select()
eg:app.window.menu_select(Edit -> Replace)

5. Click the mouse

A) click () a Button control
b) check_by_click () by a click () method of checking the checkbox
C) uncheck_by_click () () method unchecked checkbox by the Click
D) get_check_state () Returns a checked checkbox state (no tick 0 is selected, a check, variable 2)
E) is_checked (check returns true, for the check returns false, variable returns None)
F) Check () check CheckBox
G) uncheck () not checked CheckBox
H) Invoke () click (UIA the MODE)
i) Toggle () check the checkbox (uia mode)

6. keyboard

“+”:Shift
“^”:Control
“%”:Alt


Original link: https: //blog.csdn.net/weixin_40161673/article/details/83246861

Guess you like

Origin www.cnblogs.com/hester/p/11343783.html