JSHandle
表示网页上下文中对 JavaScript 对象的引用。这允许您直接从脚本中与 JavaScript 对象进行交互。
注意
此 API 正在开发中。以下某些功能可能会出现意外行为。
支持的 API
方法 | 描述 |
---|---|
asElement() | 如果对象句柄是 ElementHandle 的实例,则返回 null 或对象句柄本身。 |
dispose() | 停止引用元素句柄。 |
evaluate(pageFunction[, arg]) | 评估 pageFunction 并返回其返回值。 |
evaluateHandle(pageFunction[, arg]) | 评估 pageFunction 并返回一个 JSHandle 。 |
getProperties() | 获取一个映射,其中包含 JSHandle 自身的属性名称,以及其值作为 JSHandle 实例。 |
jsonValue() | 获取对象的 JSON 表示。 |
示例
import { browser } from 'k6/browser';
export const options = {
scenarios: {
ui: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
try {
await page.goto('https://test.k6.io/');
const jsHandle = await page.evaluateHandle(() => document.head);
// ...
} finally {
await page.close();
}
}