当前位置: 首页 > 知识库问答 >
问题:

如何使用Java滚动到Appium最新版本中的特定元素?

亢建白
2023-03-14

我的元素在屏幕上不可见,但当我使用.isDisplayed()方法时,我的代码如下

Point xy = send_mail.getCoordinates().onScreen();
        int a=xy.getX();
        int b =xy.getY();
        System.out.println("a" + xy.getX() + "b" + xy.getY());
        TouchAction touchAction = new TouchAction(driver);
        touchAction.moveTo(a, b);

send_mail是要找到的元素的xpath。这是tracelog的帮助!我不能使用while循环进行迭代,直到元素在应用程序中被视为可见时才显示出来。

java.lang.UnsupportedOperationException: Not supported yet.
    at org.openqa.selenium.remote.RemoteWebElement$1.onScreen(RemoteWebElement.java:402)
    at testcases.TestcaseFunctionsAndroid.offlinesearchservice(TestcaseFunctionsAndroid.java:764)
    at testcases.TestCasesAndroid.TC9(TestCasesAndroid.java:279)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

共有1个答案

丌官远
2023-03-14

试试这个:

以下是获取垂直元素坐标的方法

public int[] getCoordinates(String xpath, AndroidDriver<AndroidElement> driver) throws Exception {
         //Locate element for which you wants to retrieve x y coordinates.
        //int arr[] ;
         int arr[] = new int[10];
         //Get the middle co-ordinates of the element and store in  the array.
         WebElement element = driver.findElementByXPath(xpath);
         int leftX = element.getLocation().getX();
         int rightX = leftX + element.getSize().getWidth();
         int middleX = (rightX + leftX) / 2;
         int upperY = element.getLocation().getY();
         int lowerY = upperY + element.getSize().getHeight();
         int middleY = (upperY + lowerY) / 2;
         System.out.println("x co-ordinat: "+middleX+"\n y co-ordinat: "+middleY+"\n left x axis: "+leftX+"\n right x: "+rightX);
         arr[0]= middleX;
         arr[1]= middleY;
         arr[2]= rightX;
         arr[3]= leftX;
         return arr;
         }

然后使用:

            int[] co_ordinates = takeaway.getCoordinates(Xpath of from Element  , driver);
            System.out.println(" x cordinate: " + co_ordinates[0] + "\n y cordinat: " + co_ordinates[1]);
            int[] dinates = takeaway.getCoordinates(Xpath of To Element, driver);
            TouchAction touch = new TouchAction(driver);
            touch.longPress(co_ordinates[0], co_ordinates[1]).moveTo(dinates[0], dinates[1]).release().perform();
 类似资料: