开测开测 2024-08-15 15:43 采纳率: 100%
浏览 5
已结题

appium tag 方法正常使用报错

import unittest
from time import sleep
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By

# 打开相册
capabilities = {}
capabilities['platformName'] = 'Android'
capabilities['automationName'] = 'uiautomator2'
capabilities['deviceName'] = 'Android'
capabilities['appPackage'] = 'com.android.settings'
capabilities['appActivity'] = '.Settings'
capabilities['language'] = 'en'
capabilities['locale'] = 'US'

appium_server_url = 'http://localhost:4723'


class TestAppium(unittest.TestCase):

    def setUp(self) -> None:
        # 获取driver
        self.driver = webdriver.Remote(appium_server_url,capabilities)
        self.driver.implicitly_wait(30)

    def tearDown(self) -> None:
        if self.driver:
            # 退出程序
            sleep(2)
            self.driver.quit()

    def test_find_battery(self) -> None:
        # 创建touchaction对象
        el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
        TouchAction(self.driver).tap(el).perform()

if __name__ == '__main__':
    unittest.main()

里面的tap怎么不能使用了,写法正常,但运行起来的时侯,会显示报错,无法出现对应的效果,求解决

会显示下面的错误信息

"C:\tools\python 38\python.exe" "C:/tools/pycharm/PyCharm 2024.1.4/plugins/python/helpers/pycharm/_jb_unittest_runner.py" --target test_appium_tap.TestAppium 
Testing started at 下午3:41 ...
Launching unittests with arguments python -m unittest test_appium_tap.TestAppium in C:\Users\TopOn\Desktop\test_appium

C:\tools\python 38\lib\site-packages\appium\webdriver\webdriver.py:273: DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg
  super().__init__(
C:\tools\python 38\lib\site-packages\selenium\webdriver\remote\remote_connection.py:441: DeprecationWarning: HTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).
  if resp.getheader('Content-Type'):
C:\tools\python 38\lib\site-packages\selenium\webdriver\remote\remote_connection.py:442: DeprecationWarning: HTTPResponse.getheader() is deprecated and will be removed in urllib3 v2.1.0. Instead use HTTPResponse.headers.get(name, default).
  content_type = resp.getheader('Content-Type').split(';')
[Deprecated] 'TouchAction' action is deprecated. Please use W3C actions instead.

Error
Traceback (most recent call last):
  File "C:\Users\TopOn\Desktop\test_appium\test_appium_tap.py", line 36, in test_find_battery
    TouchAction(self.driver).tap(el).perform()
  File "C:\tools\python 38\lib\site-packages\appium\webdriver\common\touch_action.py", line 171, in perform
    self._driver.execute(Command.TOUCH_ACTION, params)
  File "C:\tools\python 38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 418, in execute
    self.error_handler.check_response(response)
  File "C:\tools\python 38\lib\site-packages\appium\webdriver\errorhandler.py", line 31, in check_response
    raise wde
  File "C:\tools\python 38\lib\site-packages\appium\webdriver\errorhandler.py", line 26, in check_response
    super().check_response(response)
  File "C:\tools\python 38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
Stacktrace:
io.appium.uiautomator2.common.exceptions.UnknownCommandException: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:84)
    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:764)




Ran 1 test in 15.333s

FAILED (errors=1)

进程已结束,退出代码为 1


  • 写回答

2条回答 默认 最新

  • 一直免费一直爽 2024-08-15 15:49
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    从错误信息来看,问题出在TouchAction的使用上。错误提示'TouchAction' action is deprecated. Please use W3C actions instead.表示TouchAction已经被弃用,建议使用W3C的动作。

    你可以尝试使用w3c协议来解决这个问题。首先,确保你的Appium服务器支持W3C协议。然后,修改你的代码如下:

    from appium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    
    # 其他代码保持不变
    
    class TestAppium(unittest.TestCase):
    
        def setUp(self) -> None:
            # 获取driver
            capabilities = {
                'platformName': 'Android',
                'automationName': 'uiautomator2',
                'deviceName': 'Android',
                'appPackage': 'com.android.settings',
                'appActivity': '.Settings',
                'language': 'en',
                'locale': 'US',
                'browserName': '',  # 添加这一行
            }
            self.driver = webdriver.Remote(appium_server_url, capabilities)
            self.driver.implicitly_wait(30)
    
        # 其他代码保持不变
    
        def test_find_battery(self) -> None:
            # 创建touchaction对象
            el = self.driver.find_element(By.XPATH, '//*[@text="Battery"]')
            actions = ActionChains(self.driver)  # 使用ActionChains替换TouchAction
            actions.tap(el).perform()  # 使用actions.tap()替换TouchAction().tap()
    

    这样,你就可以使用W3C的动作来替代已经弃用的TouchAction了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月23日
  • 已采纳回答 8月15日
  • 创建了问题 8月15日

悬赏问题

  • ¥15 JSzip压缩文件时如何设置解压缩密码
  • ¥66 nuxtjs3 ,老是报错,内容:我有代码仓库,如何解决?
  • ¥15 ocr识别纯数字会将数字翻转,并且会识别成字母
  • ¥30 WPF如何实现动态Y轴
  • ¥15 读取parquet文件某一列的数据但是输出是整个列名和格式
  • ¥15 机动目标 制导律建模问题
  • ¥100 求Java socks 转发实现Demo
  • ¥20 随身WiFi移动网络访问不了
  • ¥50 RAD_XE11.3获取android11手机的IMEI码
  • ¥15 linux的gcc命令报错