In this tutorial, we’ll discuss how to automate desktop applications using Selenium Python. Selenium is an open-source tool mostly used for automating web applications.
However, with the help of the Pywinauto library, you can extend the capability of Selenium to automate desktop applications as well. Here, we’ll be focusing on automating Windows-based desktop applications.
Prerequisites
Before we dive into the tutorial, make sure you have the following installed on your computer:
Step 1: Installing required packages
First, we need to install both Selenium and Pywinauto libraries. To do this, open your command prompt or terminal and run the following commands:
1 2 |
pip install selenium pip install pywinauto |
Step 2: Setting up a sample desktop application for testing
For this tutorial, we’ll be automating the built-in Notepad application on Windows. Make sure the Notepad is installed on your computer. If you’d like, you can also use another application of your choice by modifying the example code accordingly.
Step 3: Creating the Python script
Now that we have installed the necessary libraries and set up the desktop application, it’s time to write the Python script to automate the application.
Create a new Python file (e.g. desktop_app_automation.py
) and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from selenium import webdriver from pywinauto.application import Application # Launch the application app = Application(backend="uia").start("notepad.exe") # Connect to the application app_notepad = Application(backend="uia").connect(title="Untitled - Notepad") # Access the window of the application notepad_window = app_notepad.window(title="Untitled - Notepad") # Type some text in the window notepad_window.Edit.type_keys("Hello, I am automating Notepad using Selenium and Python!") # Save the file notepad_window.type_keys("%FA") save_dialog = app_notepad.window(title="Save As") # Set the file name and path save_dialog.Edit.set_text("C:\\path_to_your_directory\\output_file.txt") save_dialog.Save.click() # Close the application notepad_window.type_keys("%{F4}") |
This script will carry out the following tasks:
- Launch Notepad
- Type a text into the Notepad window
- Save the typed text into a file
- Close Notepad
Step 4: Running the script
Save your Python script and run it using the command prompt or terminal. To do this, navigate to the directory containing your script and run the following command:
1 |
python desktop_app_automation.py |
You should see the Notepad application launching, and the automation script will perform the defined tasks.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from selenium import webdriver from pywinauto.application import Application # Launch the application app = Application(backend="uia").start("notepad.exe") # Connect to the application app_notepad = Application(backend="uia").connect(title="Untitled - Notepad") # Access the window of the application notepad_window = app_notepad.window(title="Untitled - Notepad") # Type some text in the window notepad_window.Edit.type_keys("Hello, I am automating Notepad using Selenium and Python!") # Save the file notepad_window.type_keys("%FA") save_dialog = app_notepad.window(title="Save As") # Set the file name and path save_dialog.Edit.set_text("C:\\path_to_your_directory\\output_file.txt") save_dialog.Save.click() # Close the application notepad_window.type_keys("%{F4}") |
Conclusion
In this tutorial, we learned how to automate a desktop application using Selenium and Pywinauto in Python. You can now extend this methodology to automate your own desktop applications, making routine tasks more efficient and less error-prone. Keep exploring and experimenting to find additional ways to enhance your automation scripts.