跳转到主要内容
版本: 23.11.1

Page.queryObjects() 方法

此方法迭代 JavaScript 堆,并查找具有给定原型(prototype)的所有对象。

签名

class Page {
abstract queryObjects<Prototype>(
prototypeHandle: JSHandle<Prototype>,
): Promise<JSHandle<Prototype[]>>;
}

参数

参数

类型

描述

prototypeHandle

JSHandle<Prototype>

指向对象原型的句柄。

返回

Promise<JSHandle<Prototype[]>>

Promise,它解析为指向具有此原型的对象数组的句柄。

示例

// Create a Map object
await page.evaluate(() => (window.map = new Map()));
// Get a handle to the Map object prototype
const mapPrototype = await page.evaluateHandle(() => Map.prototype);
// Query all map instances into an array
const mapInstances = await page.queryObjects(mapPrototype);
// Count amount of map objects in heap
const count = await page.evaluate(maps => maps.length, mapInstances);
await mapInstances.dispose();
await mapPrototype.dispose();