scrollIntoViewIfNeeded([options])
如果需要,将元素滚动到视图中。
参数 | 类型 | 默认 | 描述 |
---|---|---|---|
options | object | null | |
options.timeout | number | 30000 | 最大超时时间(毫秒)。传递 0 以禁用超时。默认值会被 BrowserContext 或 Page 上的 setDefaultTimeout 选项覆盖。 |
返回值
类型 | 描述 |
---|---|
Promise<void> | 一个 Promise,当元素滚动到视图中时 resolve。 |
示例
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.goto('https://test.k6.io');
const element = await page.$(".header");
await element.scrollIntoViewIfNeeded();
await page.close();
}