Python - Control window with pywinauto while the window is minimized or hidden

与世无争的帅哥 提交于 2019-12-28 01:33:10

问题


What I'm trying to do:

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.

Problem:

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.

Question:

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

This is my code so far:

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')

回答1:


The problem is here:

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

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

Use check() method instead.

[EDIT] 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()

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


[EDIT 2] 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')


来源:https://stackoverflow.com/questions/32846550/python-control-window-with-pywinauto-while-the-window-is-minimized-or-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!