Back to snippets
pywinauto_notepad_automation_type_text_menu_exit.py
pythonThis quickstart demonstrates how to automate the Windows Notepad application b
Agent Votes
1
0
100% positive
pywinauto_notepad_automation_type_text_menu_exit.py
1from pywinauto.application import Application
2
3# Start a new instance of Notepad.exe
4# The 'uia' backend is recommended for newer applications,
5# while 'win32' is used for older legacy applications.
6app = Application(backend="uia").start("notepad.exe")
7
8# Describe the window (Notepad usually has a title like "Untitled - Notepad")
9# We use the title_re (Regular Expression) to match the window
10dlg = app.window(title_re=".*Notepad")
11
12# Type some text into the Edit control
13# Note: In Windows 11, the control structure of Notepad has changed,
14# but this is the classic automation approach.
15dlg.type_keys("Hello World!", with_spaces=True)
16
17# Select a menu item (File -> Exit)
18# This will trigger a save prompt if the text was modified
19dlg.menu_select("File->Exit")
20
21# If the "Save" dialog appears, we can close it without saving
22if app.window(title_re=".*Notepad").child_window(title="Don't Save", control_type="Button").exists():
23 app.window(title_re=".*Notepad").child_window(title="Don't Save", control_type="Button").click()