Of course! Let's dive into Python UI Automation.

The term "uiaotumator" seems to be a typo or a misremembering of UIAutomator. UIAutomator is a popular test automation framework, but it's important to distinguish between the original Java-based UIAutomator for Android and the modern, Python-based library called uiautomator2, which is what most developers use today.
I'll cover both, but the focus will be on the powerful and recommended Python solution.
Summary of Options
| Library/Framework | Language | Primary Use Case | Key Features |
|---|---|---|---|
uiautomator2 |
Python | Mobile App Automation (Android) | - Directly installs a helper app on the device. - Supports both real devices and emulators. - Rich API for finding elements and performing actions. - Can get the device's screen content as an image. |
Appium |
Python, Java, JS, etc. | Cross-Platform (iOS & Android) | - Industry standard for mobile automation. - Uses WebDriver protocol. - Can automate native, hybrid, and web apps. - More complex setup. |
pyautogui |
Python | Desktop Automation | - Controls the mouse and keyboard. - Works on Windows, macOS, and Linux. - Can take screenshots and find images on screen. |
Selenium |
Python, Java, JS, etc. | Web Browser Automation | - The standard for web testing. - Automates browsers like Chrome, Firefox, Safari. - Uses WebDriver protocol. |
The Modern Python Solution: uiautomator2 (For Android)
This is the most powerful and direct way to automate Android apps with Python. It works by installing a small helper app (UiAutomator2) onto your Android device, which then communicates with your Python script.
Why use uiautomator2?
- Direct Control: It bypasses layers like Appium and communicates directly with the Android UI, making it very fast and reliable.
- Rich Features: You can get element hierarchy, screenshots, network logs, and more.
- Pythonic API: The library is designed to be easy to use for Python developers.
Step-by-Step Guide with uiautomator2
Prerequisites:

- Python 3: Installed on your machine.
- Android Device or Emulator:
- Device: Enable "Developer options" and "USB debugging".
- Emulator: Use Android Studio or AVD Manager to create an emulator.
- ADB (Android Debug Bridge): Must be installed and accessible in your system's PATH.
Step 1: Install the Python Library
pip install --pre uiautomator2
Step 2: Initialize the Device
This command will install the helper app (atx-agent) on your connected device/emulator.
python -m uiautomator2 init
It will automatically detect your connected device. If you have multiple, you can specify the serial ID.
Step 3: Write Your First Python Script

Let's automate opening the calculator app, adding two numbers, and verifying the result.
import uiautomator2 as u2
# Connect to the device (first device found)
d = u2.connect()
# Optional: Take a screenshot to see the initial state
d.screenshot("initial_screen.png")
# --- Start Automation ---
# 1. Open the Calculator app
# We use the app's package name and activity name
d.app_start("com.android.calculator2")
print("Calculator app started.")
# 2. Perform a series of taps to calculate 5 + 7
# Find elements by their text (resource-id is also very reliable)
d(text="5").click()
d(text="+").click()
d(text="7").click()
d(text="=").click()
# 3. Verify the result
# Get the text of the result display element
result_element = d(resourceId="com.android.calculator2:id/result")
# The .get() method retrieves the current text
result_text = result_element.get().strip()
print(f"The result is: {result_text}")
# Assert the result
assert result_text == "12", f"Expected 12, but got {result_text}"
print("Test Passed: 5 + 7 = 12")
# --- Clean Up ---
# Stop the app
d.app_stop("com.android.calculator2")
print("Calculator app stopped.")
# Optional: Take a final screenshot
d.screenshot("final_screen.png")
Finding Elements (The Most Important Part)
uiautomator2 offers multiple strategies to find UI elements:
# Find by text (most common) d(text="OK") d(textContains="Cancel") d(textStartsWith="Insta") d(textMatches=".*Settings.*") # Uses regex # Find by resource ID (most reliable for stable elements) d(resourceId="com.example.app:id/login_button") # Find by description (for accessibility) d(description="Submit form") # Find by class name d(className="android.widget.Button") # Combine strategies (very powerful) d(text="Login", className="android.widget.Button") # Indexing (e.g., the second button) d(className="android.widget.Button")[1] # Using UI Hierarchy Inspector # Before writing a script, inspect the screen to find the best selector: d.dump_hierarchy() # Prints the XML hierarchy to the console
The Cross-Platform Solution: Appium with Python
If you need to automate both Android and iOS, Appium is the way to go. It uses the same WebDriver protocol for both platforms.
How it Works
Appium acts as a server that your Python script (using a client library like Appium-Python-Client) communicates with. This server then translates your commands into platform-specific UI actions.
Step-by-Step Guide with Appium
Prerequisites:
- Install Appium Server: Download from the official website or install via
npm(npm install -g appium). - Install Appium Python Client:
pip install Appium-Python-Client
- Setup is more complex: You need to configure your Appium session with capabilities specific to your platform (Android/iOS).
Example Python Script for Android with Appium:
This script does the same thing as the uiautomator2 example.
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
# Desired Capabilities: Tell Appium what to automate
caps = {
"platformName": "Android",
"deviceName": "Pixel_API_30", # Can be any name
"appPackage": "com.android.calculator2",
"appActivity": "com.android.calculator2.Calculator",
"automationName": "UiAutomator2" # Use the same underlying technology
}
# Start the Appium session
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
try:
# Find elements using AppiumBy
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "5").click()
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Plus").click()
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "7").click()
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Equals").click()
# Verify result
result_element = driver.find_element(AppiumBy.ID, "com.android.calculator2:id/result")
result_text = result_element.text
assert result_text == "12"
print("Appium Test Passed: 5 + 7 = 12")
finally:
# Quit the driver session
driver.quit()
For Desktop Automation: pyautogui
If your goal is to automate a desktop application (like a Windows GUI, a macOS app, or a Linux program), pyautogui is the perfect tool.
Installation:
pip install pyautogui
Example: Automating Notepad on Windows
import pyautogui
import time
# Add a safety feature: move mouse to top-left corner to abort
pyautogui.FAILSAFE = True
print("Starting script in 5 seconds...")
time.sleep(5)
# 1. Open Notepad (This part is OS-specific and might need os.system or subprocess)
# For this example, we'll assume Notepad is already open and focused.
# On Windows, you could use: os.system("notepad")
# 2. Type "Hello, World!"
pyautogui.write("Hello, World!", interval=0.1) # interval for typing speed
# 3. Press Enter
pyautogui.press('enter')
# 4. Type a second line
pyautogui.write("This is automated by PyAutoGUI.", interval=0.1)
print("Script finished.")
Conclusion and Recommendation
- For Android App Automation: Use
uiautomator2. It's modern, fast, and has a fantastic Python API. It's the go-to choice for most Python developers working on Android. - For Cross-Platform (Android & iOS) Automation: Use
Appiumwith theAppium-Python-Client. It's the industry standard, though the setup is more involved. - For Desktop Application Automation: Use
pyautogui. It's simple, powerful, and works across all major desktop operating systems. - For Web Browser Automation: Use
Selenium. It's the undisputed king of web testing.
