pywinauto Tutorial 2

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.

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

Two, wrapper function

# - * - Coding: UTF-. 8 - * - 
Import Time, OS 
from pywinauto Import file application 

'' ' 
installer, keyboard shortcuts correspondence code table as follows: 
the SHIFT + 
the CTRL ^ 
the ALT% 
spacebar {the SPACE} 
the BACKSPACE {the BACKSPACE} , the BS} or {} {BKSP 
BREAK BREAK} { 
the CAPS the LOCK CAPSLOCK} { 
DEL or the DELETE} {the DELETE or DEL} { 
DOWN ARROW DOWN} { 
the END the END {} 
the ENTER the ENTER} or {~  
the ESC the ESC} {
the HELP the HELP} {
HOME                             {HOME}
INS   or   INSERT                {INSERT}   or   {INS}
LEFT   ARROW                     {LEFT}
NUM   LOCK                       {NUMLOCK}
PAGE   DOWN                      {PGDN}
PAGE   UP                        {PGUP}
PRINT   SCREEN                   {PRTSC}
RIGHT   ARROW                    {RIGHT}
SCROLL   LOCK                    {SCROLLLOCK}
TAB                              {TAB}
UP   ARROW                       {UP}
+                                {ADD}
-                                {SUBTRACT}
*                                {MULTIPLY}
/ {Of DIVIDE} 
'' ' 

class Tool_Installer_Error(Exception):

    "" "the Application has Not been Connected to A Process yet" "" 

    Pass # the pragma: NO Cover 

class Tool_Installer (Object): 
    ' '' 
    controls a very important method wrapper_object () 
    as: DLG = object_func [ 'Button1'] wrapper_object (). 
    Print (the dir (object_func)) # print control methods are available 
    '' ' 
    DEF the __init __ (Self): 
        self.app_master = None 
        self.app_window = None 
        self.app_dlg = None 


    app_start DEF (Self, app_full_path, timeout = 0.5): 
        Print ( 'Start file application') 
        IF Not os.path.exists (app_full_path): 
            The raise Tool_Installer_Error ( "% S does Not EXISTS"% (app_full_path)) 
        Self.app_master = application.Application().start(app_full_path)
        time.sleep(timeout)

    def app_connect(self,title_re,class_name,backend='win32',timeout=0.5):
        print('get application')
        self.app_window = application.Application(backend=backend).connect(title_re=title_re, class_name=class_name,timeout=timeout)

    def app_get_dlg(self,title):
        print('get dialog on application')
        self.app_dlg = self.app_window.window(title=title)              #安装robot发现title_re参数失效

    def app_get_all_widget(self):
        print('get all widget on dialog')
        widgets = self.app_dlg.print_control_identifiers()
        return widgets

    app_wait_button_active DEF (Self, widget_name): 
        Print ( 'the wait for Active Button') 
        the while Not self.app_dlg [widget_name] .is_enabled (): 
            the time.sleep (. 3) 

    DEF app_widget_click (Self, widget_name, timeout =. 5): 
        Print ( 'the Click Button ON S%'% (widget_name)) 
        # wait smart widget appears, timeout is 5S 
        self.app_dlg [widget_name] .wait (wait_for = "visible", timeout = timeout) 
        self.app_dlg [widget_name] .click () 

    DEF app_widget_send_key (Self, widget_name, Key, timeout =. 5): 
        Print ( 'Send% S% S ON'% (Key, widget_name)) 
        # wait smart widget appears, timeout is 5S 
        self.app_dlg [widget_name] .wait (wait_for = "visible", timeout = timeout)
        self.app_dlg[widget_name].type_keys(key)

    def app_widget_get_text(self,widget_name):
        object_func = self.app_dlg[widget_name].wrapper_object()
        return object_func.window_text()

if __name__ == '__main__':
    #安装robotframework
    robot_file_path = r'D:\software\robotframework-2.8.7.win32.exe'
    robot = Tool_Installer()
    app_master = robot.app_start(robot_file_path)
    robot.app_connect('Setup','#32770')
    robot.app_get_dlg('Setup')
    robot.app_widget_send_key(widget_name='Button2',key='%N')
    robot.app_widget_send_key(widget_name='Button3',key='%N')
    robot.app_widget_send_key(widget_name='Button2',key='%N')
    print(time.time())
    print(robot.app_dlg.exists())
    print(time.time())
    robot.app_widget_send_key(widget_name='Button2',key='{ENTER}',timeout=60)

  

Guess you like

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