关于appium+python里self.driver.tap([(x,y)])无效的原因

本文探讨了在Appium结合Python进行自动化测试时,使用坐标点击方法tap遇到的问题及解决方案。作者通过添加等待时间确保页面加载完成后再执行点击操作,最终解决了tap方法无效的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于appium+python里self.driver.tap([(x,y)])无效的原因


今天写自动化,找不到元素点位,只能通过坐标去点击,于是使用tap去实现。

但是运行到tap的时候出错,提示找不到元素。去看了源码,发现并没有写错格式。

这里给个源码:

def tap(self, positions, duration=None):
    """Taps on an particular place with up to five fingers, holding for a
    certain time

    :Args:
     - positions - an array of tuples representing the x/y coordinates of
     the fingers to tap. Length can be up to five.
     - duration - (optional) length of time to tap, in ms

    :Usage:
        driver.tap([(100, 20), (100, 60), (100, 100)], 500)
    """
    if len(positions) == 1:
        action = TouchAction(self)
        x = positions[0][0]
        y = positions[0][1]
        if duration:
            action.long_press(x=x, y=y, duration=duration).release()
        else:
            action.tap(x=x, y=y)
        action.perform()
    else:
        ma = MultiAction(self)
        for position in positions:
            x = position[0]
            y = position[1]
            action = TouchAction(self)
            if duration:
                action.long_press(x=x, y=y, duration=duration).release()
            else:
                action.press(x=x, y=y).release()
            ma.add(action)

        ma.perform()
    return self


最后觉得可能是页面还没跳转就实现了tap,所有加了一个WebDriverWait去判断元素存在,最后成功实现。

因为之前一直用的click(),click是识别元素才进行点击的,然而坐标信息是一直存在的,所以tap会直接运行,前面务必加上延时或者判断!!!


修改后再次调用此方法,控制台打印下面信息,请帮我分析一下:Message: androidx.test.uiautomator.StaleObjectException Stacktrace: io.appium.uiautomator2.common.exceptions.StaleElementReferenceException: androidx.test.uiautomator.StaleObjectException at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:45) at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:262) at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:256) 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) Caused by: androidx.test.uiautomator.StaleObjectException at androidx.test.uiautomator.UiObject2.getAccessibilityNodeInfo(UiObject2.java:647) at androidx.test.uiautomator.UiObject2.hashCode(UiObject2.java:105) at java.lang.Object.toString(Object.java:299) at java.util.Formatter$FormatSpecifier.printString(Formatter.java:3138) at java.util.Formatter$FormatSpecifier.print(Formatter.java:3015) at java.util.Formatter.format(Formatter.java:2678) at java.util.Formatter.format(Formatter.java:2614) at java.lang.String.format(String.java:4017) at io.appium.uiautomator2.utils.ReflectionUtils.invoke(ReflectionUtils.java:84) at io.appium.uiautomator2.core.AxNodeInfoExtractor.extractAxNodeInfo(AxNodeInfoExtractor.java:51) at io.appium.uiautomator2.core.AxNodeInfoExtractor.toAxNodeInfo(AxNodeInfoExtractor.java:41) at io.appium.uiautomator2.utils.ElementHelpers.canSetProgress(ElementHelpers.java:77) at io.appium.uiautomator2.model.BaseElement.canSetProgress(BaseElement.java:160) at io.appium.uiautomator2.handler.SendKeysToElement.setProgress(SendKeysToElement.java:47) at io.appium.uiautomator2.handler.SendKeysToElement.safeHandle(SendKeysToElement.java:111) at io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:41) ... 33 more 调用的这个方法内容如下: def random_operation(self, duration,package_name,timeout=25): crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count = 0, 0, 0, 0, 0 # 获取设备屏幕尺寸 screen = self.driver.get_window_size() screen_width = screen['width'] screen_height = screen['height'] start_time = time.time() last_operation_time = time.time() # 记录最后一次成功操作的时间 while time.time() - start_time < duration: # 检测全局超时 if time.time() - last_operation_time > timeout: print(f"操作超时({timeout}秒无响应),结束测试") break #驻留检测 try: currentt_package = self.get_current_package().lower() target_package = package_name.lower() if currentt_package != target_package: print(f"当前包名不匹配目标!!!") self.driver.activate_app(package_name) time.sleep(3) last_operation_time = time.time() except Exception as e: print(f"驻留检测失败:{e}") try: # 随机选择操作类型(点击/滑动/输入) operation = random.choice(['click', 'swipe', 'input']) if operation == 'click': # 随机坐标点击 x = random.randint(0, screen_width) y = random.randint(0, screen_height) self.driver.tap([(x, y)], duration=50) time.sleep(0.3) elif operation == 'swipe': # 随机方向滑动(上下左右) direction = random.choice(['up', 'down', 'left', 'right']) if direction == 'up': self.driver.swipe(screen_width // 2, screen_height * 3 // 4, screen_width // 2, screen_height // 4, 500) elif direction == 'down': self.driver.swipe(screen_width // 2, screen_height // 4, screen_width // 2, screen_height * 3 // 4, 500) elif direction == 'left': self.driver.swipe(screen_width * 3 // 4, screen_height // 2, screen_width // 4, screen_height // 2, 500) elif direction == 'right': self.driver.swipe(screen_width // 4, screen_height // 2, screen_width * 3 // 4, screen_height // 2, 500) time.sleep(0.5) elif operation == 'input': # 查找输入框元素并随机输入数字 input_elements = self.driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.EditText") if input_elements: input_element = random.choice(input_elements) input_element.click() random_digits = ''.join(str(random.randint(0, 9)) for _ in range(random.randint(1, 10))) input_element.send_keys(random_digits) time.sleep(0.8) if self.check_app_crash(): crash_count += 1 if self.check_app_anr(): anr_count += 1 is_flower, is_black, is_white = self.verify_screen() if is_flower: flower_screen_count += 1 if is_black: black_screen_count += 1 if is_white: white_screen_count += 1 last_operation_time = time.time() except Exception as e: print(f"随机操作执行异常,异常信息:{e}") return (crash_count, anr_count, flower_screen_count, black_screen_count, white_screen_count)
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值