如果遇到返回的宽度为负数
例如:print(ele.size)
解决方案:
desired_caps[‘automationName‘] = ‘uiautomator2‘
修改底层测试库配置为‘uiautomator2‘,
这个应该是appium优化过的底层库,修改了一些BUG,因此大家在驱动手机app遇到一些异常情况,可以考虑使用此方法,如果不设置,默认用的就是‘uiautomator‘(可能会有些问题)
如果将desired_caps[‘automationName‘] = ‘uiautomator2‘设置后出现以下这个问题:
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate. Original error Command ‘‘D:\\Program Files (x86)\\Java\\jdk1.8.0_172\\bin\\java.exe‘ -jar ‘C:\\Program Files\\Appium\\resources\\app\\node_modules\\appium\\node_modules\\appium-adb\\jars\\sign.jar‘ ‘C:\\Program Files\\Appium\\resources\\app\\node_modules\\appium\\node_modules\\appium-uiautomator2-server\\apks\\appium-uiautomator2-server-debug-androidTest.apk‘ --override‘ exited with code 1
重新安装appium,将其安装在个人账户下就好了
appium-desktop github官网:https://github.com/appium/appium-desktop/releases/
代码重新运行下,正常了ing........(亲测有效)
代码如下:
from appium import webdriver
import time
import traceback
desired_caps = {}
desired_caps[‘platformName‘] = ‘Android‘ #测试平台
desired_caps[‘platformVersion‘] = ‘5.1‘ #平台版本,不能写错
desired_caps[‘deviceName‘] = ‘test‘ #设备名称,多设备时需区分
# desired_caps[‘app‘] = r‘d:\apk\HiSpace.apk‘ #app 文件 名,指定了要安装的app 在电脑上的路径
desired_caps[‘appPackage‘] = ‘com.huawei.appmarket‘ #app package名,指定了要运行的app
desired_caps[‘appActivity‘] = ‘com.huawei.appmarket.MainActivity‘ #app默认Activity
desired_caps[‘unicodeKeyboard‘] = True # 一定要有该参数,否则unicode 输入的中文无效
desired_caps[‘automationName‘] = ‘uiautomator2‘
desired_caps[‘noReset‘] = True
desired_caps[‘newCommandTimeout‘] = 60
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps) #启动Remote RPC
driver.implicitly_wait(10)
try:
ele = driver.find_element_by_id(‘com.huawei.appmarket:id/backPicture‘)
time.sleep(2)
location = ele.location
size1 = ele.size
print(location, size1)
x1 = location[‘x‘] + int(size1[‘width‘] * 0.8)
x2 = location[‘x‘] + int(size1[‘width‘] * 0.2)
y = location[‘y‘] + int(size1[‘height‘] * 0.5)
for i in range(10):
driver.swipe(x1, y, x2, y, 500)
time.sleep(0.5)
except:
print(traceback.format_exc())
input(‘**** Press to quit..‘)
driver.quit()