跳到主要内容

滚动到视图

将元素滚动到桌面/移动Web 以及移动原生应用的视口中。

信息

移动原生应用的滚动基于移动swipe命令实现。

此命令仅适用于以下更新的组件:

  • Appium服务器(版本2.0.0或更高)
  • appium-uiautomator2-driver(用于Android)
  • appium-xcuitest-driver(用于iOS)

确保您的本地或基于云的Appium环境定期更新以避免兼容性问题。

参数
名称类型详情
options
可选
object, booleanElement.scrollIntoView()的选项。桌面/移动Web的默认值:
{ block: 'start', inline: 'nearest' }
移动原生应用的默认值
{ maxScrolls: 10, scrollDirection: 'down' }
仅限桌面/移动Web
options.behavior
可选
string参见 MDN 参考
仅限WEB (桌面/移动)
options.block
可选
string参见 MDN 参考
仅限WEB (桌面/移动)
options.inline
可选
string参见 MDN 参考
仅限WEB (桌面/移动)
仅限移动原生应用
options.direction
可选
string可以是downupleftright之一,默认为up
仅限移动原生应用
options.maxScrolls
可选
number在停止搜索元素之前的最大滚动次数,默认为10
仅限移动原生应用
options.duration
可选
number滑动的持续时间(毫秒)。默认为1500毫秒。值越低,滑动越快。
仅限移动原生应用
options.scrollableElement
可选
Element用于在其中滚动的元素。如果未提供元素,它将对iOS使用以下选择器-ios predicate string:type == "XCUIElementTypeApplication",对Android使用以下选择器//android.widget.ScrollView'。如果有多个元素匹配默认选择器,则默认情况下它将选择第一个匹配的元素。
仅限移动原生应用
options.percent
可选
number要滑动的(默认)可滚动元素的百分比。这是一个介于0和1之间的值。默认为0.95
切勿从屏幕的确切顶部|底部|左侧|右侧滑动,您可能会触发例如通知栏或其他操作系统/应用功能,这可能导致意外结果。
仅限移动原生应用
示例
desktop.mobile.web.scrollIntoView.js
it('should demonstrate the desktop/mobile web scrollIntoView command', async () => {
const elem = await $('#myElement');
// scroll to specific element
await elem.scrollIntoView();
// center element within the viewport
await elem.scrollIntoView({ block: 'center', inline: 'center' });
});

mobile.native.app.scrollIntoView.js
it('should demonstrate the mobile native app scrollIntoView command', async () => {
const elem = await $('#myElement');
// scroll to a specific element in the default scrollable element for Android or iOS for a maximum of 10 scrolls
await elem.scrollIntoView();
// Scroll to the left in the scrollable element called '#scrollable' for a maximum of 5 scrolls
await elem.scrollIntoView({
direction: 'left',
maxScrolls: 5,
scrollableElement: $('#scrollable')
});
});

Welcome! How can I help?

WebdriverIO AI Copilot