Appium Python Client
The Appium Python Client is a client library for Appium, a mobile automation framework that allows testing of native, hybrid, and mobile web apps. It wraps standard Selenium WebDriver commands to provide Appium-specific extensions for mobile gestures and functionalities. The current version is 5.3.0, and it maintains a frequent release cadence, often addressing bug fixes and minor enhancements.
Warnings
- breaking The `desired_capabilities` dictionary for `webdriver.Remote` has been replaced by `options` objects (e.g., `AppiumOptions`). Passing a dictionary directly will likely lead to errors or unexpected behavior.
- breaking Direct `find_element_by_*` methods (e.g., `find_element_by_id`, `find_element_by_xpath`) are deprecated and largely removed, aligning with Selenium 4 changes.
- gotcha For Appium server 2.x, the default URL changed from `http://localhost:4723/wd/hub` to simply `http://localhost:4723`. Using the old `/wd/hub` path will result in connection failures.
- gotcha The Appium server (the Node.js application) must be installed and running independently before you can execute any Python client scripts. The Python client acts as a bridge to the server.
- gotcha While `AppiumOptions` is a base class, for real-world scenarios, it's often more appropriate and robust to use platform-specific option classes like `UiAutomator2Options` (for Android) or `XCUITestOptions` (for iOS). These provide specific capabilities and validations.
Install
-
pip install Appium-Python-Client
Imports
- webdriver
from appium import webdriver
- AppiumOptions
from appium.options.common.base import AppiumOptions
- AppiumBy
from appium.webdriver.common.appiumby import AppiumBy
Quickstart
import os
from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
APPIUM_SERVER_URL = os.environ.get('APPIUM_SERVER_URL', 'http://localhost:4723')
options = AppiumOptions()
options.platformName = os.environ.get('APPIUM_PLATFORM_NAME', 'Android')
options.automationName = os.environ.get('APPIUM_AUTOMATION_NAME', 'UiAutomator2')
options.deviceName = os.environ.get('APPIUM_DEVICE_NAME', 'Android Emulator')
options.appPackage = os.environ.get('APPIUM_APP_PACKAGE', 'com.android.settings')
options.appActivity = os.environ.get('APPIUM_APP_ACTIVITY', '.Settings')
print(f"Connecting to Appium server at: {APPIUM_SERVER_URL}")
print(f"Using capabilities: {options.to_capabilities()}")
driver = None
try:
driver = webdriver.Remote(APPIUM_SERVER_URL, options=options)
print("Driver initialized successfully.")
# Example action: Find and click 'Display' setting
# Requires an Android emulator/device with the default Settings app
el = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Display")
print(f"Found element: {el.text}")
el.click()
print("Clicked 'Display' setting.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if driver:
driver.quit()
print("Driver quit.")