Python - 在窗口最小化或隐藏时使用 pywinauto 控制窗口 [英] Python - Control window with pywinauto while the window is minimized or hidden

查看:73
本文介绍了Python - 在窗口最小化或隐藏时使用 pywinauto 控制窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

我正在尝试使用 pywinauto 在 python 中创建一个脚本,以在后台自动安装 notepad++(隐藏或最小化),notepad++ 只是一个示例,因为我将对其进行编辑以与其他软件一起使用.

I'm trying to create a script in python with pywinauto to automatically install notepad++ in the background (hidden or minimized), notepad++ is just an example since I will edit it to work with other software.

问题:

问题是我想在安装程序隐藏或最小化时执行此操作,但是如果我移动鼠标,脚本将停止工作.

The problem is that I want to do it while the installer is hidden or minimized, but if I move my mouse the script will stop working.

问题:

如何在 notepad++ 安装程序隐藏或最小化的情况下执行此脚本并使其工作.

How can I execute this script and make it work, while the notepad++ installer is hidden or minimized.

这是我目前的代码:

import sys, os, pywinauto

pwa_app = pywinauto.application.Application()

app = pywinauto.Application().Start(r'npp.6.8.3.Installer.exe')

Wizard = app['Installer Language']

Wizard.NextButton.Click()

Wizard = app['Notepad++ v6.8.3 Setup']

Wizard.Wait('visible')

Wizard['Welcome to the Notepad++ v6.8.3 Setup'].Wait('ready')
Wizard.NextButton.Click()

Wizard['License Agreement'].Wait('ready')
Wizard['I &Agree'].Click()

Wizard['Choose Install Location'].Wait('ready')
Wizard.Button2.Click()

Wizard['Choose Components'].Wait('ready')
Wizard.Button2.Click()

Wizard['Create Shortcut on Desktop'].Wait('enabled').CheckByClick()
Wizard.Install.Click()

Wizard['Completing the Notepad++ v6.8.3 Setup'].Wait('ready', timeout=30)
Wizard['CheckBox'].Wait('enabled').Click()
Wizard.Finish.Click()
Wizard.WaitNot('visible')

推荐答案

问题出在这里:

Wizard['Create Shortcut on Desktop'].wait('enabled').check_by_click()

check_by_click() 使用 click_input() 方法移动真实的鼠标光标并执行真实的点击.

check_by_click() uses click_input() method that moves real mouse cursor and performs a realistic click.

改用 check() 方法.

如果安装程序没有正确处理 BM_SETCHECK,解决方法可能如下所示:

If the installer doesn't handle BM_SETCHECK properly the workaround may look so:

checkbox = Wizard['Create Shortcut on Desktop'].wait('enabled')
if checkbox.get_check_state() != pywinauto.win32defines.BST_CHECKED:
    checkbox.click()

我将在下一个 pywinauto 版本中通过分别创建方法 check_by_clickcheck_by_click_input 来修复它.

I will fix it in the next pywinauto release by creating methods check_by_click and check_by_click_input respectively.

我使用我的修复程序尝试了您的脚本,无论是否移动鼠标,它都可以完美运行(并且非常快).Win7 x64,32 位 Python 2.7,pywinauto 0.6.x,以管理员身份运行.

I tried your script with my fix and it works perfectly (and very fast) with and without mouse moves. Win7 x64, 32-bit Python 2.7, pywinauto 0.6.x, run as administrator.

import sys
import os
from pywinauto import Application

app = Application(backend="win32").start(r'npp.6.8.3.Installer.exe')

Wizard = app['Installer Language']

Wizard.minimize()
Wizard.NextButton.click()

Wizard = app['Notepad++ v6.8.3 Setup']

Wizard.wait('visible')
Wizard.minimize()

Wizard['Welcome to the Notepad++ v6.8.3 Setup'].wait('ready')
Wizard.NextButton.click()

Wizard.minimize()
Wizard['License Agreement'].wait('ready')
Wizard['I &Agree'].click()

Wizard.minimize()
Wizard['Choose Install Location'].wait('ready')
Wizard.Button2.click()

Wizard.minimize()
Wizard['Choose Components'].wait('ready')
Wizard.Button2.click()

Wizard.minimize()
checkbox = Wizard['Create Shortcut on Desktop'].wait('enabled')
if checkbox.get_check_state() != pywinauto.win32defines.BST_CHECKED:
    checkbox.click()
Wizard.Install.click()

Wizard['Completing the Notepad++ v6.8.3 Setup'].wait('ready', timeout=30)
Wizard.minimize()
Wizard['CheckBox'].wait('enabled').click()
Wizard.Finish.click()
Wizard.wait_not('visible')

这篇关于Python - 在窗口最小化或隐藏时使用 pywinauto 控制窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆