1.简介

经过上一篇的学习、介绍和了解,想必小伙伴或者童鞋们,已经见识到pywinauto的强大了,今天继续介绍pywinauto,上一篇已经可以打开计算器了,这里宏哥在提供其他方法进行打开计算器和非电脑自带程序。pywinauto 可以启动电脑自带的应用程序,也可以启动电脑安装的应用程序。

2.运行指定的应用程序

运行指定的应用程序,顾名思义就是用户指定运行那款程序,就运行那款程序。宏哥这里将其分为两大类:电脑自带的应用程序,如:记事本、计算器等和电脑安装的应用程序,如:QQ、微信等。分好后,接下来看宏哥是如何这两类应用程序的。

2.1启动电脑自带的应用程序

上一篇已经启动计算器了,今天宏哥启动一下记事本,给小伙伴或者童鞋们来演示一下。

通过start() 方法指定exe应用程序的名称即可。start()方法用于启动一个可执行程序

    def start(self, cmd_line, timeout=None, retry_interval=None,
              create_new_console=False, wait_for_idle=True, work_dir=None):
        """Start the application as specified by cmd_line"""
        # try to parse executable name and check it has correct bitness
        if '.exe' in cmd_line and self.backend.name == 'win32':
            exe_name = cmd_line.split('.exe')[0] + '.exe'
            _warn_incorrect_binary_bitness(exe_name)

        if timeout is None:
            timeout = Timings.app_start_timeout
        if retry_interval is None:
            retry_interval = Timings.app_start_retry

        start_info = win32process.STARTUPINFO()

        # we need to wrap the command line as it can be modified
        # by the function
        command_line = cmd_line

        # Actually create the process
        dw_creation_flags = 0
        if create_new_console:
            dw_creation_flags = win32con.CREATE_NEW_CONSOLE
        try:
            (h_process, _, dw_process_id, _) = win32process.CreateProcess(
                None,                     # module name
                command_line,            # command line
                None,                     # Process handle not inheritable.
                None,                     # Thread handle not inheritable.
                0,                         # Set handle inheritance to FALSE.
                dw_creation_flags,        # Creation flags.
                None,                     # Use parent's environment block.
                work_dir,                # If None - use parent's starting directory.
                start_info)                # STARTUPINFO structure.
        except Exception as exc:
            # if it failed for some reason
            message = ('Could not create the process "%s"\n'
                       'Error returned by CreateProcess: %s') % (cmd_line, str(exc))
            raise AppStartError(message)

        self.process = dw_process_id

        if self.backend.name == 'win32':
            self.__warn_incorrect_bitness()

        def app_idle():
            """Return true when the application is ready to start"""
            result = win32event.WaitForInputIdle(
                h_process, int(timeout * 1000))

            # wait completed successfully
            if result == 0:
                return True

            # the wait returned because it timed out
            if result == win32con.WAIT_TIMEOUT:
                return False

            return bool(self.windows())

        # Wait until the application is ready after starting it
        if wait_for_idle and not app_idle():
            warnings.warn('Application is not loaded correctly (WaitForInputIdle failed)', RuntimeWarning)

        self.actions.log("Started " + cmd_line + " application.")

        return self
04-15 15:34