首页 > 解决方案 > 如何在appium-android中长按元素?

问题描述

appium=1.9.0
安卓设备
windows 10

长按元素不起作用

我已经尝试过:

  1. <MobileElement longpress = (MobileElement) new WebDriverWait(driver, 30).
        until(ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("msgContent")));
    new Actions(driver).clickAndHold(longpress).perform();
    

    错误:

org.openqa.selenium.InvalidArgumentException:参数不正确。我们想要 {"required":["actions"]} 而你发送了 ["element"]

  1. TouchAction action = new TouchAction(driver);
    action.longPress((LongPressOptions) element).release().perform();
    

    错误:

java.lang.ClassCastException:com.sun.proxy.$Proxy16 无法转换为 io.appium.java_client.touch.LongPressOptions

  1. new TouchAction(driver).press(ElementOption.element(element)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(20))).release().perform();
    

错误:

java.lang.ClassCastException:无法将 com.sun.proxy.$Proxy16 转换为 org.openqa.selenium.internal.HasIdentity

标签: appium-android

解决方案


而不是在“按下”传递元素传递元素的坐标,检查下面的代码

  1. 首先找到你的元素
  2. 在 press 中传递元素的 x 和 y 坐标

    WebElement ele = driver.findElement(BY.xpath("your_element_xpath"));
    Point location = ele.getLocation();
    new TouchAction(driver).press(PointOption.point(location.getX(), location.getY())).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(20))).release().perform();
    

此代码应将元素保留 20 秒


推荐阅读