python写窗口程序_Python的 - 与pywinauto控制窗口,而窗口最小化或隐藏

1586010002-jmsa.png

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

解决方案

The problem is here:

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

CheckByClick() uses ClickInput() 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.GetCheckState() != pywinauto.win32defines.BST_CHECKED:

checkbox.Click()

I will fix it in the next pywinauto release by creating methods CheckByClick and CheckByClickInput 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.5.3, run as administrator.

import sys, os, pywinauto

app = pywinauto.Application().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.GetCheckState() != 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.WaitNot('visible')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值