Skip to content

Instantly share code, notes, and snippets.

@vasily-v-ryabov
Last active May 31, 2021 08:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vasily-v-ryabov/e5f927908d424bad422b to your computer and use it in GitHub Desktop.
Save vasily-v-ryabov/e5f927908d424bad422b to your computer and use it in GitHub Desktop.
Uninstall script for 7zip 9.20 (64-bit) - this is just an example
# Requirements:
# - Win7 or Win8.1 x64, 64-bit Python
# - pywinauto 0.5.2+
# - UAC is fully disabled
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()
@Ahsanmumtaz1105
Copy link

I'm trying to use as an example on windows 10. However, on line no. 21 item_7z = ProgramsAndFeatures.FolderView.GetItem('7-Zip 9.20 (x64 edition)') getting exception.
-> item_7z = ProgramsAndFeatures.FolderView.GetItem('7-Zip 16.04 (x64)')
(Pdb) n
pywinauto.findwindows.ElementNotFoundError: {'top_level_only': True, 'active_only': True, 'title': 'Programs and Features', 'class_name': 'CabinetWClass', 'backend': 'win32', 'process': 6148}

c:\imaging\poc\winium\winium_desktop\winium_samples\sample_tests\ee_sample_uninstall_1.py(33)()
-> item_7z = ProgramsAndFeatures.FolderView.GetItem('7-Zip 16.04 (x64)')

@fionazzz
Copy link

fionazzz commented Jun 6, 2018

Change the scripts to uninstall my own app,but sometimes get error below.
RuntimeError: We should never get to this part of ListView.get_item(), retval = 0, GetLastError() = 5, item_index = 39, subitem_index = 0

The error occured when running:
if '7-Zip 9.20 (x64 edition)' not in ProgramsAndFeatures.FolderView.Texts(): print('OK')
I tried to wait a few seconds before this, but the problem still happens occasionally.
Tried on Windows 7.

@renatoramossilva
Copy link

I am trying this example and I have the follow error:

ValueError: '7-Zip 16.04 (x64)' is not in list. But I have this 7zip version installed in my machine.

I tryed to show the list using: item_7z = ProgramsAndFeatures.FolderView.texts()#GetItem('7-Zip 16.04 (x64)')
print(item_7z)

But the list is empty:

['FolderView', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

Can anyone help me?

@vasily-v-ryabov
Copy link
Author

Hi @renatoramossilva maybe this example is a little bit outdated because Win10 had several updates (probably in UI as well). I'm planning to re-check all the examples before next major release (probably this winter).

Anyway full traceback with the error would be useful.

@rajeshpachaikani
Copy link

@vasily-v-ryabov Traceback when running the above script in PyWinAuto version 0.6.8 and windows 20H2(OS Build 19042.985)

C:/Users/admin/PycharmProjects/Tool_sanity/scratch_py.py:8: DeprecationWarning: Method .Start() is deprecated, use .start() instead.
pywinauto.Application().Start(r'explorer.exe')
C:/Users/admin/PycharmProjects/Tool_sanity/scratch_py.py:9: DeprecationWarning: Method .Connect() is deprecated, use .connect() instead.
explorer = pywinauto.Application().Connect(path='explorer.exe')
C:/Users/admin/PycharmProjects/Tool_sanity/scratch_py.py:12: DeprecationWarning: Method .Window_() is deprecated, use .window() instead.
NewWindow = explorer.Window_(top_level_only=True, active_only=True, class_name='CabinetWClass')
Traceback (most recent call last):
File "C:\Python38\lib\site-packages\pywinauto\application.py", line 250, in __resolve_control
ctrl = wait_until_passes(
File "C:\Python38\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
raise err
pywinauto.timings.TimeoutError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):

File "C:/Users/admin/PycharmProjects/Tool_sanity/scratch_py.py", line 42, in
NewWindow.Close()
File "C:\Python38\lib\site-packages\pywinauto\application.py", line 396, in getattribute
ctrls = self.__resolve_control(self.criteria)
File "C:\Python38\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
raise e.original_exception
File "C:\Python38\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
func_val = func(*args, **kwargs)
File "C:\Python38\lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
File "C:\Python38\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
raise ElementNotFoundError(kwargs)
pywinauto.findwindows.ElementNotFoundError: {'top_level_only': True, 'active_only': True, 'class_name': 'CabinetWClass', 'backend': 'win32', 'process': 20396}

Process finished with exit code 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment