Puppeter-如何遍历queryObjects来收集WebSocket对象的url



我在Node.js模块中使用Puppeter。我使用queryObjects检索WebSocket对象原型,并需要提取url属性。

// Get a handle to the websocket object prototype

const prototypeHandle = await page.evaluateHandle(() => WebSocket.prototype);

// Query all websocket instances into a jsHandle object

const jsHandle = await page.queryObjects(prototypeHandle);

// Count amount of map objects in heap

// const count = await page.evaluate(maps => maps.length, jsHandle); // returns the expected amount (x2)
// How to iterate through jsHandle to collect the url of each websockets
await jsHandle.dispose();
await prototypeHandle.dispose();

您没有得到任何响应,因为WebSocket不是一个简单的JSON对象,当您使用page.evaluate.进行评估时,它可以被字符串化并返回给您

要获取页面中连接的websocket的URL,可以映射收集的WebSocketinstance/objects并从中提取URL。

const browser = await puppeteer.launch();
const page = (await browser.pages())[0];
// create a dummy websocket connection for testing purpose
await page.evaluate(() => new WebSocket('wss://echo.websocket.org/'));

const wsPrototypeHandle = await page.evaluateHandle(
() => WebSocket.prototype
);
const wsInstances = await page.queryObjects(wsPrototypeHandle);
const wsUrls = await page.evaluate(
(e) => e.map((e) => e['url']), // <-- simply access the object here
wsInstances
);
console.log(wsUrls);

这将导致以下情况,

[ 'wss://echo.websocket.org/' ]

最新更新