How to scroll in mobile browser using Appium and selenium?

时间:2017-11-13 06:16:07

标签: selenium appium mobile-browser

How to scroll in mobile browser using Appium and selenium? Scroll is working in app but not in browser. Using scrollToExact method. web app was developed using ionic framework

2 个答案:

答案 0 :(得分:1)

有几种方法可以完成它。

如果您使用的是 AppiumDriver 的实例,则需要切换到原生视图才能使用TouchAction

driver.context("NATIVE_APP");
// Get your screen size to set properly start point (startX, startY)
// and end point (endX, endY) for scrolling
Dimension screenSize = driver.manage().window().getSize();
new TouchAction(driver)
  .press(<startX>, <startY>)
  .waitAction(500)
  .press(<endX>, <endY>)
  .release()
  .perform();

如果您使用的是 RemoteWebDriver 的实例,那么您可以这样做:

driver.get("https://www.google.de");
ExecuteMethod method = new RemoteExecuteMethod(driver);
RemoteTouchScreen screen = new RemoteTouchScreen(method);
screen.up(10, 20);

答案 1 :(得分:0)

多亏了 dmle,我发现您需要先切换到本机视图,然后才能使用 TouchAction。无论如何,该代码不起作用,因为 press 方法不接受两个 int 值。

在这里,我分享了在我的案例中有效的代码,以及如何通过在更改为本机上下文之前保存其值来使用 Web 上下文元素作为参考。我也恢复了以前的上下文:

//Get web context element references to touch on its area
MobileElement tmpElement = driver.findElement(by);
int x=tmpElement.getLocation().x; 
int y=tmpElement.getLocation().y;
Dimension elemSize = tmpElement.getSize();
int height=elemSize.height;

//Save precious context
String previousContext=driver.getContext();
//Set native context
driver.context("NATIVE_APP");   
//Perform scroll
new TouchAction(driver)
    .press(ElementOption.point(x+5, y+height-5))
    .waitAction(WaitOptions.waitOptions(ofSeconds(1)))
    .moveTo(ElementOption.point(x+5, y+5))
    .release()
    .perform();
//Restore context
driver.context(previousContext);