Puppeteer - 如何遍历 queryObjects 以收集 WebSocket 对象的 url?
Puppeteer - how to iterate through queryObjects to collect the url of a WebSocket object?
我在 Node.js 模块中使用 Puppeteer。我用 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,可以通过收集的WebSocket映射instances/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/' ]
我在 Node.js 模块中使用 Puppeteer。我用 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,可以通过收集的WebSocket映射instances/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/' ]