pywinauto.actionlogger.enable()
. from __future__ import print_function # for py2/py3 compaibility import sys, os # assume the installer is placed in the same folder as the script os.chdir(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0]))) import pywinauto app = pywinauto.Application().Start(r'msiexec.exe /i 7z920-x64.msi') Wizard = app['7-Zip 9.20 (x64 edition) Setup'] Wizard.NextButton.Click() Wizard['I &accept the terms in the License Agreement'].Wait('enabled').CheckByClick() Wizard.NextButton.Click() Wizard['Custom Setup'].Wait('enabled') Wizard.NextButton.Click() Wizard.Install.Click() Wizard.Finish.Wait('enabled', timeout=30) Wizard.Finish.Click() Wizard.WaitNot('visible') if os.path.exists(r"C:\Program Files\7-Zip\7zFM.exe"): print('OK') else: print('FAIL')
Wizard.Finish
object Wizard.Finish
is just a button description, and it is not associated with a real button until Wait () or any other method is called. Strictly speaking, a Wizard.Finish.Click()
call is equivalent to a longer Wizard.Finish.WrapperObject().Click()
call (it usually occurs implicitly) and is almost equivalent to Wizard.Finish.Wait('enabled').Click()
. It was possible to write in one line, but sometimes it is worth emphasizing the importance of the Wait () method. from __future__ import print_function import pywinauto pywinauto.Application().Start(r'explorer.exe') explorer = pywinauto.Application().Connect(path='explorer.exe') # Go to "Control Panel -> Programs and Features" NewWindow = explorer.Window_(top_level_only=True, active_only=True, class_name='CabinetWClass') try: NewWindow.AddressBandRoot.ClickInput() NewWindow.TypeKeys(r'Control Panel\Programs\Programs and Features{ENTER}', with_spaces=True, set_foreground=False) ProgramsAndFeatures = explorer.Window_(top_level_only=True, active_only=True, title='Programs and Features', class_name='CabinetWClass') # wait while list of programs is loading explorer.WaitCPUUsageLower(threshold=5) item_7z = ProgramsAndFeatures.FolderView.GetItem('7-Zip 9.20 (x64 edition)') item_7z.EnsureVisible() item_7z.ClickInput(button='right', where='icon') explorer.PopupMenu.MenuItem('Uninstall').Click() Confirmation = explorer.Window_(title='Programs and Features', class_name='#32770', active_only=True) if Confirmation.Exists(): Confirmation.Yes.ClickInput() Confirmation.WaitNot('visible') WindowsInstaller = explorer.Window_(title='Windows Installer', class_name='#32770', active_only=True) if WindowsInstaller.Exists(): WindowsInstaller.WaitNot('visible', timeout=20) SevenZipInstaller = explorer.Window_(title='7-Zip 9.20 (x64 edition)', class_name='#32770', active_only=True) if SevenZipInstaller.Exists(): SevenZipInstaller.WaitNot('visible', timeout=20) if '7-Zip 9.20 (x64 edition)' not in ProgramsAndFeatures.FolderView.Texts(): print('OK') finally: NewWindow.Close()
AddressBandRoot
), the so-called in-place edit box appears (only at the time of entry). When calling the TypeKeys()
method, TypeKeys()
must specify the parameter set_foreground=False
(appeared in 0.5.0), otherwise the in-place edit box will disappear from the radar. For all in-place controls it is recommended to set this parameter to False.ProgramsAndFeatures.FolderView.Wait('enabled')
does not guarantee that it has already been completely initialized. Lazy (lazy) initialization goes in a separate thread, so you need to monitor the CPU activity of the entire explorer.exe process. To do this, pywinauto 0.5.2 implements two methods: CPUUsage()
, which returns the CPU load in percent, and WaitCPUUsageLower()
, waiting until the CPU load falls below the threshold (2.5% by default). The idea of ​​implementing these methods was suggested by the article comrade JOHN_16: “We monitor the completion of the CPU loading process” .item_7z.EnsureVisible()
magically scrolls the list so that the desired item is visible. No special work with the scroll bar is needed. wmic product where name="7-Zip 9.20 (x64 edition)" call uninstall
Source: https://habr.com/ru/post/266459/
All Articles