本篇文章给大家谈谈python控制windows窗口,并输入数据,以及python控制windows窗口,希望对各位有所帮助,不要忘了收藏本站喔。

What I'm trying to do:

I'm trying to create a 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 will stop working.

Question:

How can I execute this 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'')

Wizard = app['Installer Language']

Wizard.NextButton.Click()

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

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

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

Wizard = app['Installer Language']

Wizard.Minimize()

Wizard.NextButton.Click()

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

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

Not('visible')

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐