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

gau*_*dey 5 java android selenium-webdriver appium

我正在尝试向下滚动并单击设置中的某个元素,但由于 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();

}

}
Run Code Online (Sandbox Code Playgroud)

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

MobileElement radioGroup = (MobileElement) wd

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

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

            + "new UiSelector().text(\"Feature\"));");
            radioGroup.click();
Run Code Online (Sandbox Code Playgroud)

我也试过这种方式,但它没有滚动......设置打开,就是这样

Abh*_*nav 5

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

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); 
}
Run Code Online (Sandbox Code Playgroud)

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

while(condition){
    scroll();
}
Run Code Online (Sandbox Code Playgroud)

PS:您可以根据需要修改 0.5 的值,我在我的情况下根据需要使用了该值。