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

如何使用TouchAction滚动Appium 1.7.1

郗学
2023-03-14

在iOS和Android应用程序中,我无法向下滚动到某个元素。由于从Appium 1.6.3更新到1.7.1,从io.Appium更新到6.1.0,因此不推荐使用滑动方法,唯一的解决方案是使用TouchActions。

我试图用TouchActions解决它,但它根本没有滚动,或者滚动方向错误。

到目前为止,我的解决方案是这样的,也许有人能解释我做错了什么:

public void scrollDownUntilElementVisible(WebElement element){
    TouchAction touchAction = new TouchAction(getDriver());
    for(int i=0; i<dimensions.getHeight();i++){
       if(element.isDisplayed()){
          break;
       }else{
          touchAction.press(0,0).moveTo(element).release().perform();
       }
    }
}

这不是完整的代码,但我希望你明白。

如果我使用x,y坐标,而不是我在示例中寻找的webElement,它将如何工作?它不像以前版本中的swipe方法那样工作,或者我做得不对。也许有人能解释一下。

共有2个答案

毕衡
2023-03-14

我需要滚动找到屏幕外的元素。我想到的是:

  1. 在当前/可见屏幕上搜索所需元素。
private void scrollDown() {
    //if pressX was zero it didn't work for me
    int pressX = driver.manage().window().getSize().width / 2;
    // 4/5 of the screen as the bottom finger-press point
    int bottomY = driver.manage().window().getSize().height * 4/5;
    // just non zero point, as it didn't scroll to zero normally
    int topY = driver.manage().window().getSize().height / 8;
    //scroll with TouchAction by itself
    scroll(pressX, bottomY, pressX, topY);
}

/*
 * don't forget that it's "natural scroll" where 
 * fromY is the point where you press the and toY where you release it
 */
private void scroll(int fromX, int fromY, int toX, int toY) {
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(fromX, fromY).moveTo(toX, toY).release().perform();
}

另外,您可以从元素中获取坐标并在滚动中使用它。

P. S. S.我用了appium 1.6.5

章涵蓄
2023-03-14

在最新版本的Appium需要添加(PointOption.point同时传递坐标)一些使用TouchAction滚动的代码:

private void scrollDown() {
    //if pressX was zero it didn't work for me
    int pressX = driver.manage().window().getSize().width / 2;
    // 4/5 of the screen as the bottom finger-press point
    int bottomY = driver.manage().window().getSize().height * 4/5;
    // just non zero point, as it didn't scroll to zero normally
    int topY = driver.manage().window().getSize().height / 8;
    //scroll with TouchAction by itself
    scroll(pressX, bottomY, pressX, topY);
}



private void scroll(int fromX, int fromY, int toX, int toY) {
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(PointOption.point(fromX, fromY)).moveTo(PointOption.point(toX, toY)).release().perform();
}
 类似资料:
  • 问题内容: 我在向下滚动到iOS和Android应用程序中的某个元素时遇到麻烦。由于从Appium 1.6.3更新到1.7.1,将io.appium更新到6.1.0,因此不建议使用swipe方法,唯一的解决方案是使用TouchActions。 我尝试使用TouchActions解决它,但是它根本没有滚动,或者滚动方向错误。 到目前为止,我的解决方案看起来像这样,也许有人可以解释我做错了什么: 它不

  • 问题内容: 我有个问题。 我有一个带有一些JTextField,JLabel,Jlists和JButtons的JFrame,现在我框架的内容超出了屏幕区域,因此我想将JScrollBar附加到我的JFrame上,但是滚动条不起作用。因此,有人可以指导我如何使用JScrollbar滚动JFrame吗? 问题答案: 将所有组件放在一个面板中(而不是在JFrame中) 将此面板添加到JScrollPan

  • 目前,我正在为ios ionic应用程序编写一个Appium脚本,我使用以下方法来实现滑动功能。 在上面的方法中,单词press,waitAction

  • 我正在尝试使用pyqt5创建一个垂直布局的滚动区,我正在里面放一些标签。我知道,即使是垂直布局,如果文本不适合,也应该水平滚动。但无论我尝试什么,它都不让我滚动。 这是我正在使用的代码: 下面是它的外观: 下面是它的样子: 我希望我的问题是清楚的

  • 如何禁用滚动?

  • 问题内容: 我有一个,当行太多时,会出现滚动条,但是当行太长时,该行会分成两行,而不是出现水平滚动条,如何使水平条出现而不是分成两行,我的添加如下: 问题答案: 正如我们自己的Rob Camick 在这里介绍的那样,您可以尝试使用类似… 这将停止行/自动换行