使用Python+Appium做移动端浏览器自动化时遇URI与Package参数必填错误
问题详情
执行Python+Appium移动端浏览器自动化测试时,出现以下错误:
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: URI and package arguments are required Stacktrace:UnknownError: An unknown server-side error occurred while processing the command. Original error: URI and package arguments are required
at getResponseForW3CError (C:\Program Files\Appium Server GUI\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
at asyncHandler (C:\Program Files\Appium Server GUI\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
用户源代码:
from appium import webdriver import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.wait import WebDriverWait desired_caps = { 'platformName': 'Android', 'deviceName': 'AQE6MVWWAEVOZLTO-r1HMVZ', 'udid': '22e051b70d1783d3', 'appPackage': 'com.android.chrome', 'appActivity': 'com.google.android.apps.chrome.Main', 'showChromedriverLog': True, 'automationName': 'UiAutomator2', 'noReset': False } web_driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # Open a website web_driver.get('https://www.google.com') web_driver.find_element(By.XPATH, '//*[@id="js-search-autocomplete"]').send_keys("USA") wait = WebDriverWait(web_driver, 10) date = wait.until(ec.element_to_be_clickable((By.XPATH, '//*[@id="js-date-range-display"]'))) date.click() time.sleep(125) web_driver.execute_script('mobile: shell', {'command': 'pm', 'args': ['clear', 'https://www.rentbyowner.com']}) # Close the browser web_driver.quit()
用户疑问:是否是Desired Capabilities中遗漏了配置项?
问题原因与解决方法
核心错误原因
报错和Desired Capabilities无关,问题出在mobile: shell执行的pm clear命令参数错误:
pm clear的正确语法是pm clear <应用包名>,你传入的是网址https://www.rentbyowner.com,不是合法的包名,导致命令执行失败- 若要清除Chrome浏览器缓存,需传入Chrome的包名
com.android.chrome
其他代码问题修正
- 重复导入冲突:同时从
appium和selenium导入webdriver,会覆盖对象导致异常,保留from appium import webdriver即可 - XPath转义字符错误:代码中的
"是HTML转义双引号,需改为正常双引号",否则元素定位会失败 - 元素定位与页面不匹配:原代码里的元素ID是针对
rentbyowner.com的,但你先打开的是google.com,这会导致定位失败,需根据实际访问页面调整定位表达式
修正后的完整代码
from appium import webdriver import time from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.wait import WebDriverWait desired_caps = { 'platformName': 'Android', 'deviceName': 'AQE6MVWWAEVOZLTO-r1HMVZ', 'udid': '22e051b70d1783d3', 'appPackage': 'com.android.chrome', 'appActivity': 'com.google.android.apps.chrome.Main', 'showChromedriverLog': True, 'automationName': 'UiAutomator2', 'noReset': False } web_driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 直接访问目标网站,匹配后续元素定位 web_driver.get('https://www.rentbyowner.com') # 搜索框定位 web_driver.find_element(By.XPATH, '//*[@id="js-search-autocomplete"]').send_keys("USA") wait = WebDriverWait(web_driver, 10) # 等待日期选择元素可点击 date = wait.until(ec.element_to_be_clickable((By.XPATH, '//*[@id="js-date-range-display"]'))) date.click() time.sleep(5) # 修正pm clear命令,传入Chrome包名 web_driver.execute_script('mobile: shell', {'command': 'pm', 'args': ['clear', 'com.android.chrome']}) # 关闭浏览器 web_driver.quit()
内容的提问来源于stack exchange,提问作者Mahboob Nur

