8

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')
Vasily Ryabov
  • 7,764
  • 6
  • 21
  • 61
Carlos Barros
  • 199
  • 1
  • 4
  • 10

1 Answers1

8

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')
Vasily Ryabov
  • 7,764
  • 6
  • 21
  • 61
  • 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 Sep 29 '15 at 16:11
  • 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? – Carlos Barros Sep 29 '15 at 18:12
  • 1
    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. – Vasily Ryabov Sep 30 '15 at 08:05
  • 1
    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](http://pywinauto.github.io/docs/code/pywinauto.application.html?highlight=wait#pywinauto.application.WindowSpecification.Wait) 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. – moden Sep 30 '15 at 08:07
  • 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 Sep 30 '15 at 13:46
  • by the way, I'm using Windows 10 Pro x64, python 3.4, pywinauto 0.5.3 as administrator – Carlos Barros Sep 30 '15 at 15:53
  • Carlos, I could not find how to hide the window from the taskbar. If you could, it would be great feature request! ;-) – Vasily Ryabov Oct 01 '15 at 05:25
  • Well, thank you for all your help! You helped me a lot! If I find a way of hiding it I will tell you ;) – Carlos Barros Oct 01 '15 at 10:14
  • 1
    The following thread could be useful: http://stackoverflow.com/questions/19425038/hide-window-from-ms-windows-taskbar I cannot get it to work yet, but it seems like right direction. – Vasily Ryabov Oct 02 '15 at 11:41
  • 1
    This is another useful question: http://stackoverflow.com/questions/30933219/hide-window-from-taskbar-without-using-ws-ex-toolwindow – Vasily Ryabov Oct 02 '15 at 11:58
  • OK, I figured it out and will implement `HideFromTaskbar` method of the `DialogWrapper`. – Vasily Ryabov Oct 02 '15 at 12:10
  • 2
    https://github.com/pywinauto/pywinauto/commit/46cd14110abd309dd72a172bb0879931b2f84d0b – Vasily Ryabov Oct 02 '15 at 13:05
  • 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.? – Carlos Barros Oct 02 '15 at 21:34