1.appium-python-client 修改版本1.5 為5.1.1,后執行python程序時,提示: raise TypeError( TypeError: missing 1 required keyword-only argument: 'options' (instance of driver `options.Options` class)
你遇到的錯誤:
TypeError: missing 1 required keyword-only argument: 'options'
(instance of driver `options.Options` class)
📌 錯誤原因分析
這是 Appium-Python-Client 版本升級帶來的 API 不兼容問題。
你從版本 1.5 升級到了 5.1.1,這是一個非常大的版本跳躍,其中 Appium 客戶端庫進行了重大更新(主要是與 Selenium 的整合和重構)。
🔍 舊版 vs 新版 WebDriver 初始化方式對比
? 舊版(Appium-Python-Client < 3.x)
from appium import webdriverdesired_caps = {'platformName': 'Android','deviceName': 'emulator-5554','appPackage': 'com.example.app','appActivity': '.MainActivity'
}driver = webdriver.Remote('http://localhost:4723', desired_caps)
這個寫法在新版本中已經不被支持了!
? 新版(Appium-Python-Client ≥ 5.0)
現在要求使用 options
參數來代替直接傳入 desired_caps
,這與新版 Selenium 的風格一致。
? 正確修改方法(適用于 Appium-Python-Client 5.1.1)
你需要將原來的 desired_caps
放入一個 options
對象中。
? 修改后的代碼如下:
from appium import webdriver
from appium.options.android import UiAutomator2Options# 設置 Desired Capabilities
desired_caps = {'platformName': 'Android','deviceName': 'emulator-5554','appPackage': 'com.example.app','appActivity': '.MainActivity','automationName': 'UiAutomator2','noReset': True
}# 使用新的 Options 類封裝
options = UiAutomator2Options().load_capabilities(desired_caps)# 創建 WebDriver 實例
driver = webdriver.Remote(command_executor='http://localhost:4723',options=options
)print("Driver started:", driver)
🧪 常見注意事項
注意事項 | 說明 |
---|---|
UiAutomator2Options | Android 測試時推薦使用這個類 |
options ?是必須參數 | 新版要求必須使用?options ?而不是?desired_caps |
command_executor | 就是你的 Appium Server 地址:http://localhost:4723 |
? 如何降級回舊版本?(可選)
如果你暫時不想改代碼,可以考慮降級到舊版本:
pip install Appium-Python-Client==1.5.0
但這只是臨時方案,建議盡快遷移到新版 API。
? 總結
問題 | 解決方法 |
---|---|
TypeError: missing keyword-only argument 'options' | 使用?options ?參數代替?desired_caps |
Appium Python Client 5.1.1 兼容性問題 | 使用?UiAutomator2Options.load_capabilities() ?包裝?desired_caps |
推薦做法 | 使用新版 API,適配未來更新 |
如果你愿意貼出你的完整代碼片段,我可以幫你逐行修改成兼容 5.1.1 的寫法。歡迎繼續提問!