On longPress in Android device(Google Pixel) - InvalidElementStateException: Unable to perform W3C actions. Check logcat output

@Aleksei I’ m getting this error: org.openqa.selenium.InvalidStateException: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid.

I’m using appium 2.0 and performing a longPress action on an element where I’m getting this issue intermittently. The device being used in Google Pixel on Android version 12.

Any idea of what could be going wrong??

No idea without your code including 1-2 actions before.
0 ios or android ?
1 how you find element ?
2 how you make long tap ?


0: Its an Android device - Google Pixel and Android version 12

1: I use @AndroidFindBy annotation implementing the pagefactory model to find the element:-
@AndroidFindBy(uiAutomator = “new UiSelector().descriptionContains("App Info")”)
WebElement appInfoBtn;

2: Use the below longPress() method to implement the same with below code snippet:
public void longPress(AppiumDriver driver, WebElement ele) {
Point location = ele.getLocation();
Dimension size = ele.getSize();
Point centreofElem = getCenterofElem(location, size);

    PointerInput pressFinger = new PointerInput(PointerInput.Kind.TOUCH,"finger");
    Sequence seq = new Sequence(pressFinger, 1)
            .addAction(pressFinger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centreofElem))
            .addAction(new Pause(pressFinger, Duration.ofSeconds(1)))


and you get exception on below line?

Yes, exactly it fails on this very line: driver.perform(Collections.singletonList(seq));

Also, this is the below code to getCenterofElement:

private Point getCenterofElem(Point locn, Dimension size) {
int leftX = locn.getX();
int midX = leftX + (size.getWidth()/2);

int upperY = locn.getY();
int midY = upperY + (size.getHeight()/2);

return new Point(midX, midY);

Did you try add simple sleep for e.g. 5 sec before LongTap on element BUT after needed screen loaded?
Just to check…

@Aleksei So what are u suggesting in this case?? Should I add some sleep before performing the longPress?? In case so should I add it before calling the longPress() method or before this line- driver.perform(Collections.singletonList(seq))

Post this longPress method, I wait for the next screen 1st element visibility (which is basically a textField) by using a fluentwait and Expectedconditions.visibilityofelemntLocated() so I guess that should take care of screenLoad check.

You did not answer: does simple sleep helps you?

I still hardly believe that InvalidStateException happens inside driver.perform(Collections.singletonList(seq));. As far as NO element here and long tap happens by coordinates.

I haven’t added any sleep as of now and that’s what I asked u previously…Do I need to add sleep and if so then at which step??

Should I add it before longPress?? And if so then at which point?? after getting the co-ordinates or before getting it??

see question above.

Also you can try other solution → appium-uiautomator2-driver/docs/android-mobile-gestures.md at master · appium/appium-uiautomator2-driver · GitHub

I did try with sleep and that isn’t working out…But with the other suggestion of yours related to mobile gesture seems to work fine for now…Let me try with that for couple of runs on Jenkins…I hope so that should be stable and work smoothly !!

Thanks much for your suggestion !! Will update on this thread if the mobile gesture is working seamlessly for this issue !!