Facebook WebDriverAgent Python Client
facebook-wda is a Python client library for Facebook's WebDriverAgent, an iOS test automation framework that enables UI testing and remote control of iOS devices and simulators. It provides a programmatic interface to interact with iOS applications, perform gestures, take screenshots, and manage app states. The library is actively maintained, with frequent releases, and is currently at version 1.5.4.
Common errors
-
wda.WDAError: ...
cause The Python client failed to communicate with the WebDriverAgent server. This usually indicates WDA is not running, is unreachable, or an API call failed on the WDA side.fixVerify WebDriverAgent is running on the device/simulator and is accessible at the specified URL (e.g., `http://localhost:8100`). Check network connectivity and WDA logs for errors. Ensure no system pop-ups are blocking WDA startup on the device. -
wda.exceptions.WDAElementNotFoundError: ...
cause The requested UI element could not be found on the screen within the default timeout. This often happens if the app state is not as expected or the selector is incorrect.fixInspect the current screen's UI hierarchy (e.g., using Appium Inspector or `wda.Client().screenshot()`) to verify the element's presence and its attributes. Adjust element locators for accuracy or increase the wait timeout for the element. Use `.exists` for conditional checks or `.wait()` with a timeout. -
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:'-[PDSpecificationsContentView title]:unrecognized selector sent to instance0x...' or similar NSInvalidArgumentException
cause This error originates from WebDriverAgent itself, indicating an attempt to call a method on an object that does not respond to that selector. It often points to issues within the WDA server or the iOS app under test.fixThis is typically a WDA or iOS-level issue rather than a `facebook-wda` library issue. Try restarting WebDriverAgent, the device/simulator, or updating WDA. Check if the error is reproducible with different WDA versions or on a different device. -
invalid session id
cause The WDA session you were using became invalid, likely due to the WebDriverAgent server restarting, the app crashing, or the session timing out.fixCatch this exception and re-establish a new session. It's good practice to wrap operations in `try...except wda.WDAError` blocks to handle such transient failures gracefully.
Warnings
- breaking Starting from version 1.5.4, the `tidevice` dependency has been removed from `facebook-wda`. If your workflow previously relied on `facebook-wda` to implicitly manage or interact with `tidevice` for USB connections or WebDriverAgent startup, you will now need to manage `tidevice` and WebDriverAgent separately.
- gotcha WebDriverAgent (WDA) must be running on the target iOS device or simulator *before* `facebook-wda` can connect to it. `facebook-wda` is a client, not a server manager, and does not automatically launch WDA.
- gotcha The `window_size()` method returns dimensions in UIKit points, while `screenshot()` captures images in native resolution (pixels). These dimensions can differ, especially on Retina displays, requiring scaling when comparing or performing calculations.
- deprecated The `session.set_alert_callback()` method is deprecated and may not work as expected or might be removed in future versions.
Install
-
pip install facebook-wda
Imports
- Client
import wda c = wda.Client()
Quickstart
import wda
import os
# Get WDA server URL from environment variable, default to localhost
# Ensure WebDriverAgent is running on your iOS device/simulator, e.g., via Xcode or another tool.
wda_url = os.environ.get('WDA_URL', 'http://localhost:8100')
c = wda.Client(wda_url)
print(f"Connecting to WDA at: {wda_url}")
try:
status = c.status()
print(f"WDA Status: {status['state']}")
# Example: Press home button
c.home()
# Example: Start a session with an app (e.g., Safari)
with c.session('com.apple.mobilesafari') as s:
print(f"Session orientation: {s.orientation}")
print(f"Window size (UIKit points): {s.window_size()}")
s.open_url("https://www.google.com")
# Take a screenshot (requires Pillow to save to file)
s.screenshot('safari_screen.png')
print("Screenshot saved to safari_screen.png")
except wda.WDAError as e:
print(f"Failed to connect or interact with WDA: {e}")
print("Ensure WebDriverAgent is running and accessible at the specified URL.")