如何设置Appium UiAutomator2中UiScrollable默认从下往上滑动查找元素?
解决UiScrollable优先从下往上滑动查找元素的问题
默认情况下,UiScrollable的scrollIntoView方法会优先从上往下滑动(向上滚动列表),要让它优先以从下往上滑动(向下滚动列表)的方式查找元素,可通过以下方式修改:
简洁实现方案
直接在UiScrollable实例后调用setScrollingDirection方法指定滚动方向:
@AndroidFindBy(uiAutomator = "new UiScrollable(new UiSelector().scrollable(true)).setScrollingDirection(1).scrollIntoView(new UiSelector().description(\"Description\"))") private WebElement elementToScrollTo; elementToScrollTo.click();
关键说明
setScrollingDirection(1):参数1代表反向滚动(即从下往上滑动,对应列表向下滚动),默认参数0为正向滚动(从上往下滑动)。设置后,UiScrollable会优先以反向滚动逻辑查找目标元素,避免触发页面刷新。
自定义循环控制方案
如果需要更精细的滚动逻辑控制,也可以通过循环调用scrollBackward()实现:
@AndroidFindBy(uiAutomator = "var scrollable = new UiScrollable(new UiSelector().scrollable(true)); while (!scrollable.hasChildByDescription(\"Description\")) { scrollable.scrollBackward(); } scrollable.getChildByDescription(\"Description\")") private WebElement elementToScrollTo;
内容的提问来源于stack exchange,提问作者cdub
相关产品推荐
相关产品推荐

