android appium 中 webView 的 ScrollTo 和 ScrollToExact 等价物

ScrollTo and ScrollToExact equivalent for webView in appium for android

我正在使用 appium (java) 自动化 android 应用程序。
为了在本机组件上滚动,我使用 scrollTo()scrollToExact() 方法。
但是如果应用内有 webView ;这些方法不起作用。

更新: 我也试过这两种方法:

public static void scrollNavigation(AppiumDriver<MobileElement> wd, String widID, String target, String direction)
    {
        JavascriptExecutor js = (JavascriptExecutor) wd;
        HashMap<String, String> swipeObject = new HashMap<String, String>();
        swipeObject.put("text", target);
        swipeObject.put("direction", direction);
        swipeObject.put("element", widID);
        js.executeScript("mobile: scrollTo", swipeObject);
        wait(200);
    }

    public static void swipeUpElement(AppiumDriver<MobileElement> driver, MobileElement element, int duration)
    {
        int topY = element.getLocation().getY();
        int bottomY = topY + element.getSize().getHeight();
        int centerX = element.getLocation().getX() + (element.getSize().getWidth()/2);
        driver.swipe(centerX, bottomY, centerX, topY, duration);
    }

但是在这两个中我都需要元素-但是在页面上我找不到除 webview 之外的任何元素。
我正在使用 UiAutomator 来捕获 ID。
任何建议/link / 解决方法会有帮助

尝试在滚动之前切换到本机上下文 driver.context("NATIVE_APP");。 如果它不起作用,请尝试 JavascriptExecutor,如下例

MobileElement element = driver.findElement(By.xpath("element-xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap scrollObject = new HashMap();
scrollObject.put("direction", "down");
scrollObject.put("element", element);
js.executeScript("mobile: scroll", scrollObject);

元素在 webview 中不是唯一的位移。你需要开发数学逻辑来获取屏幕尺寸并根据它们滚动,使用下面的代码,

public void scroll() throws IOException {
          try {
            Dimension dimensions = driver.manage().window().getSize();
            System.out.println("Size of Window= " +dimension);
            int scrollStart = (int) (dimension.getHeight() * 0.5);
            System.out.println("Size of scrollStart= " +scrollStart);
            int scrollEnd = (int) (dimension.getHeight() * 0.2);
            System.out.println("Size of cscrollEnd= " + scrollEnd);             
            driver.swipe(0,scrollStart,0,scrollEnd,1000);           
            Application_Log.info("Screen Swiped " );            
            
            } catch (IOException e) {
                
                Assert.fail("Swipe failed");
            }
            
      }