【发布时间】:2021-03-09 10:25:05
【问题描述】:
如何使用 Appium 和 selenium 在移动浏览器中滚动?滚动在应用程序中工作,但不在浏览器中。使用 scrollToExact 方法。 Web 应用是使用 ionic 框架开发的
【问题讨论】:
-
你能发布你尝试过的东西吗?
标签: selenium appium mobile-browser
如何使用 Appium 和 selenium 在移动浏览器中滚动?滚动在应用程序中工作,但不在浏览器中。使用 scrollToExact 方法。 Web 应用是使用 ionic 框架开发的
【问题讨论】:
标签: selenium appium mobile-browser
有几种方法可以完成它。
如果你使用的是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);
【讨论】:
感谢 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);
【讨论】: