sushmitha m
sushmitha m

Reputation: 1

How to do a long press on element in appium-android?

appium=1.9.0
android devices
windows 10

Long press on a element is not working

I have already tried with :

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

    error:

org.openqa.selenium.InvalidArgumentException: Parameters were incorrect. We wanted {"required":["actions"]} and you sent ["element"]

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

    error:

java.lang.ClassCastException: com.sun.proxy.$Proxy16 cannot be cast to io.appium.java_client.touch.LongPressOptions

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

error:

java.lang.ClassCastException: Cannot cast com.sun.proxy.$Proxy16 to org.openqa.selenium.internal.HasIdentity

Upvotes: 0

Views: 2125

Answers (2)

Margarit Holm
Margarit Holm

Reputation: 11

The solution given works for IOS devices but not for android. For android you have to usegetCenter method instead of getLocation

Upvotes: 0

Suhail Ahmed
Suhail Ahmed

Reputation: 554

Instead of passing the element in "press" pass the coordinates of the element, check below code

  1. First locate your element
  2. Pass the x and y coordinates of the element in press

    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();
    

this code should hold the element for 20 seconds

Upvotes: 1

Related Questions