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

18,280

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')
Share:
18,280
Carlos Barros
Author by

Carlos Barros

Updated on July 25, 2022

Comments

  • Carlos Barros
    Carlos Barros almost 2 years

    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')
    
  • Carlos Barros
    Carlos Barros over 8 years
    Still doesn't work... when I execute the script if I move the mouse it will stop and not click any button. And If I dont move the mouse it will stop in 'Choose Install Location', if I move it will stop when I move the mouse.
  • Carlos Barros
    Carlos Barros over 8 years
    I just find out that if I have the mouse on the window without moving the mouse, the script will stop working, but if I have the mouse not on the window, then it will work, so my problem now is that I hadn't figure out yet how to hide the window. Can you help me on that?
  • Vasily Ryabov
    Vasily Ryabov over 8 years
    It works for me with Wizard.Minimize(). I've edited the answer with the updated script. If you still stuck there, please provide the stdout/stderr.
  • moden
    moden over 8 years
    Carlos, please, show the exact place where the script stops. I guess it happens on a .Wait('ready'). Here ready means that the window is visible and enabled - doc As you are trying to hide the window you may use .Wait('enabled') instead. Although I do not know why it affects by mouse. In any case, the list of actions and the traceback will be useful.
  • Carlos Barros
    Carlos Barros over 8 years
    It seems that the script is working pretty good now, I don't know what it was. Just one more question if it is possible, there is a way of hiding the window completely, and not show in the taskbar?
  • Carlos Barros
    Carlos Barros over 8 years
    by the way, I'm using Windows 10 Pro x64, python 3.4, pywinauto 0.5.3 as administrator
  • Vasily Ryabov
    Vasily Ryabov over 8 years
    Carlos, I could not find how to hide the window from the taskbar. If you could, it would be great feature request! ;-)
  • Carlos Barros
    Carlos Barros over 8 years
    Well, thank you for all your help! You helped me a lot! If I find a way of hiding it I will tell you ;)
  • Vasily Ryabov
    Vasily Ryabov over 8 years
    The following thread could be useful: stackoverflow.com/questions/19425038/… I cannot get it to work yet, but it seems like right direction.
  • Vasily Ryabov
    Vasily Ryabov over 8 years
    This is another useful question: stackoverflow.com/questions/30933219/…
  • Vasily Ryabov
    Vasily Ryabov over 8 years
    OK, I figured it out and will implement HideFromTaskbar method of the DialogWrapper.
  • Vasily Ryabov
    Vasily Ryabov over 8 years
  • Carlos Barros
    Carlos Barros over 8 years
    The Hide taskbar function it's working, I just tested. Also I tried win32functions.ShowWindow(self, win32defines.SW_HIDE), the window hides but the automation doesn't work. Is there any way to hide it like that and still press the buttons, etc.?