How to scroll to click an element in 2019

I order to click an element you need to swipe until the element become visible. There are several older postings on how to archive this.

However as methods where depreciated those old examples aren’t working any more and now I’m trying to work out how to perform that task in 2019.

Currently I am using the following pice of code to do the swipe:

            var touchAction = new OpenQA.Selenium.Appium.MultiTouch.TouchAction (driver);

            touchAction.Press (fromX, fromY);
            touchAction.MoveTo (toX, toY);
            touchAction.Release ();
            touchAction.Perform ();

However, this is not interpreted as a swipe but as a fling which result to scrolling the content all the way to the bottom. It’s not possible to click an element in the middle that way.

I also tried to add some waits in the hope that then it won’t be interpreted as a fling.

            var touchAction = new Appium_MultiTouch.TouchAction (driver);

            touchAction.Press (fromX, fromY);
            touchAction.Wait (ms:  1000);
            touchAction.MoveTo (toX, toY);
            touchAction.Wait (ms: 1000);
            touchAction.Release ();
            touchAction.Wait (ms: 1000);
            touchAction.Perform ();

But that doesn’t work either. Now it won’t scroll at all. Does anybody know how to do this properly?

PS: such a common task should be part of the library.

I had previously tried Swipe using Appium with older release,

private void swipeToRemove(WebElement element) {
TouchAction myAction = new TouchAction((MobileDriver)driver);
Dimension size = driver.manage().window().getSize();

    int startY = (int) (size.height * 0.);
    int endY = (int) (size.width * 0.05);
    int coordX = (int) (size.width * 0.50);"X = " + size.width + "; Y" + size.height );
    myAction.longPress(element, 3000).moveTo(coordX,endY).release().perform();

There might be some changes in Appium function in newer release. But this worked atleast in the release I attempted.

I also suggest you to try test record the swipe using Test recording and see how the code is generated.

AppiumStudio generates:

            driver.Swipe(608, 2057, 646, 933, 4681);

The swipe has by now been removed from the API. The recorder is so incredibly outdated it has become useless.

Which Appium Studio are you using … ? Appium Sudio generates TouchAction which I have already copied in different thread.

Please check this image below which I had swiped…

Same Version: 12.9.5840 (12.9.5840) — But I’m not using Java. (the var keyword in the initial post was a dead giveaway for that).

So, it’s the C# code generator which is hopelessly outdated :disappointed: and useless. I guess I try out the Java code manually converting it to C# :tired_face:

So, Java generates:

        new TouchAction(driver).press(653, 2055).waitAction(Duration.ofMillis(3117)).moveTo(686, 1155).release().perform();

But the C# API has no waitAction method — well there is a wait method and seem to work.

Final version is:

            var manage = driver.Manage ();
            var window = manage.Window;
            var size = window.Size;
            var fromX = (int)(size.Width * 0.5f);
            var fromY = (int)(size.Width - size.Width * 0.4f);
            var toX = (int)(size.Width * 0.5f);
            var toY = (int)(size.Height * 0.4f);
            var touchAction = new OpenQA.Selenium.Appium.MultiTouch.TouchAction (touchActionPerformer: driver);

            touchAction.Press (x: fromX, y: fromY);
            touchAction.Wait (ms: 2000);
            touchAction.MoveTo (x: toX, y: toY);
            touchAction.Release ();
            touchAction.Perform ();

So difference between current code and previous code is less waits

Yes. Pretty strange. Still, the generated Java code helped as it showed which wait is the relevant one.

Yes this was my observation as while swiping (as an action) we do a very small wait which we do not realize. But recording also tracks the time