mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Implement page.$$ method (#463)
This patch implements page.$$ which runs document.querySelectorAll in page and returns results as an array of ElementHandle instances. Fixes #384.
This commit is contained in:
@@ -191,10 +191,33 @@ class Frame {
|
||||
const remoteObject = await this._rawEvaluate(selector => document.querySelector(selector), selector);
|
||||
if (remoteObject.subtype === 'node')
|
||||
return new ElementHandle(this._client, remoteObject, this._mouse);
|
||||
helper.releaseObject(this._client, remoteObject);
|
||||
await helper.releaseObject(this._client, remoteObject);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} selector
|
||||
* @return {!Promise<!Array<!ElementHandle>>}
|
||||
*/
|
||||
async $$(selector) {
|
||||
const remoteObject = await this._rawEvaluate(selector => Array.from(document.querySelectorAll(selector)), selector);
|
||||
const response = await this._client.send('Runtime.getProperties', {
|
||||
objectId: remoteObject.objectId,
|
||||
ownProperties: true
|
||||
});
|
||||
const properties = response.result;
|
||||
const result = [];
|
||||
const releasePromises = [helper.releaseObject(this._client, remoteObject)];
|
||||
for (const property of properties) {
|
||||
if (property.enumerable && property.value.subtype === 'node')
|
||||
result.push(new ElementHandle(this._client, property.value, this._mouse));
|
||||
else
|
||||
releasePromises.push(helper.releaseObject(this._client, property.value));
|
||||
}
|
||||
await Promise.all(releasePromises);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()|string} pageFunction
|
||||
* @param {!Array<*>} args
|
||||
|
||||
Reference in New Issue
Block a user