python+Appium 滑动界面操作遇到取的元素宽度为负数

博客针对Appium驱动手机app时遇到的异常情况给出解决方案。当返回宽度为负数,可将底层测试库配置修改为'uiautomator2';若设置后出现特定错误,重新安装appium到个人账户下,重新运行代码即可解决。

如果遇到返回的宽度为负数

例如: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()

转载于:https://www.cnblogs.com/mimkmimk789/p/10548163.html

帮我分析下,这段代码运行报错的根因Message: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid. Stacktrace: io.appium.uiautomator2.common.exceptions.InvalidElementStateException: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid. at io.appium.uiautomator2.handler.W3CActions.safeHandle(W3CActions.java:87) at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59) at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:266) at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:260) at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:435) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:250) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:266) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:345) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1294) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:911) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:611) at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:552) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:466) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:438) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140) at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) at java.lang.Thread.run(Thread.java:1024) Traceback: File "D:\Hy_autotest\cases\ThirdApp_Test\TopApp_Compatibility\AG_download_app.py", line 86, in teststeps result = utils.download_AG_app(driver, app_name, package_name) File "D:\Hy_autotest\lib\ThirdApp\common.py", line 844, in download_AG_app self.swipe_up(driver, swipe_times=3) File "D:\Hy_autotest\lib\ThirdApp\common.py", line 788, in swipe_up driver.swipe(x1, y1, x2, y2, duration) File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\appium\webdriver\extensions\action_helpers.py", line 158, in swipe actions.perform() File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\selenium\webdriver\common\action_chains.py", line 78, in perform self.w3c_actions.perform() File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 88, in perform self.driver.execute(Command.W3C_ACTIONS, enc) File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute self.error_handler.check_response(response) File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\appium\webdriver\errorhandler.py", line 30, in check_response raise wde File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\appium\webdriver\errorhandler.py", line 26, in check_response super().check_response(response) File "E:\auto_deploy_hytest\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidElementStateException: Message: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid. Stacktrace: io.appium.uiautomator2.common.exceptions.InvalidElementStateException: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid.
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值