如何使用 Appium 向下滚动以单击特定元素,因为 scrollTo 不起作用

How to Scroll down to click a particular element using Appium as scrollTo is not working

我正在尝试向下滚动并单击设置中的某个元素,但由于 scrollTo 不起作用,我无法继续

package Test1;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.net.URL;
public class MobileFindJavaTest {
private AppiumDriver<WebElement> driver;

@BeforeMethod
 @Test
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Apps/slide/");
File app = new File(appDir, "ApiDemos-debug.apk");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","BECUPJTWGA7HAQQK");
capabilities.setCapability("platformVersion", "5");

capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), c         Capabilities);


    //driver.scrollTo("Views");
    TouchAction touchAction4 = new TouchAction(driver);
    touchAction4.press(30,40).moveTo(89,51).release();
    driver.performTouchAction(touchAction4);
    System.out.println("gaurav");
    driver.findElementByName("Feature").click();

}

}

driver.scrollTo 显示不支持,现在如何向下滚动并单击,因为我要单击的元素不在屏幕上,我们需要滚动才能找到它

MobileElement radioGroup = (MobileElement) wd

            .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"

            + ".resourceId(\"com.android.settings:id/title\")).scrollIntoView("

            + "new UiSelector().text(\"Feature\"));");
            radioGroup.click();

我也尝试过这种方式,但它没有滚动...设置打开,仅此而已

"Feature" text 为显示文字或内容描述

如果其显示文字:

MobileElement radioGroup = (MobileElement) wd

.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"

+ ".resourceId(\"<listview_id>\")).scrollIntoView("

+ "new UiSelector().text(\"Feature\"));");
radioGroup.click();

这将滚动到显示 "Feature" 文本的位置,然后单击 "Feature" 文本。但是我需要解释一件事,你只能在实现了滚动功能的视图上滚动,而不是按钮、文本等。

如果其内容描述:

wd.scrollToExact("Feature")

更多信息,请观看此视频:

https://www.youtube.com/watch?v=bT3tqaLNn-Y

点击文字:

MobileElement textLocartor = (MobileElement) wd

.findElementByAndroidUIAutomator("new UiSelector().text(\"<text_value>\")");

textLocartor.click();

您可以使用以下代码片段进行滚动:

public void scroll() {
    Dimension dimensions = driver.manage().window().getSize();
    int Startpoint = (int) (dimensions.getHeight() * 0.5);
    int scrollEnd = (int) (dimensions.getHeight() * 0.5);
    driver.swipe(200, Startpoint,200,scrollEnd,2000); 
}

您可以有一些条件可以调用此方法,一旦满足您的条件,滚动将停止,您可以执行所需的操作。类似于:

while(condition){
    scroll();
}

P.S.: 0.5的值你可以根据需要修改,我用的是我案例中需要的值。

对于 ScrollTo() 函数,以下方法非常有效:

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"_input your text name here_\"));").click();

请尝试使用上述方法 & 这应该可以解决您关于 scrollTo() 功能的问题,谢谢。

// 创建方法名称 scrollTo 或您选择的任何名称,使用下面提到的代码,它将采用我们要滚动使用的文本参数:

public void scrollTo(String text)
{                
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))");
}

这个有效

 driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"_TAGS_\"));");

将 wd 驱动程序用于 java 脚本,以下滚动方法对我有用

await driver.execute('mobile: scroll', {'direction': 'down'});

以下是我为那些使用 Appium Java 客户端版本 6 以上的人找到的最佳单行解决方案--

driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))"));