跳到主要内容

滚动到视图

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

信息

移动原生应用的滚动是基于移动swipe命令完成的。

用法
$(selector).scrollIntoView({ behavior, block, inline, direction, maxScrolls, duration, scrollableElement, percent })
参数
名称类型详情
options
可选
object, booleanElement.scrollIntoView()的选项。桌面/移动网页默认值:
{ block: 'start', inline: 'nearest' }
移动原生应用默认值
{ maxScrolls: 10, scrollDirection: 'down' }
仅桌面/移动网页
options.behavior
可选
string参见 MDN 参考
仅限网页 (桌面/移动)
options.block
可选
string参见 MDN 参考
仅限网页 (桌面/移动)
options.inline
可选
string参见 MDN 参考
仅限网页 (桌面/移动)
仅移动原生应用
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