Puppeteer 中文文档v25.8.0

Page.queryObjects() 方法

此方法遍历 JavaScript 堆,并查找所有具有给定原型的对象。

签名

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();