feat(ElementHandle): add ElementHandle.$ and ElementHandle.$$ (#1151)

This patch adds `ElementHandle.$` and `ElementHandle.$$` methods to query nested
elements.

Fixes #508
This commit is contained in:
Adam Stankiewicz
2017-10-27 11:08:58 +02:00
committed by Andrey Lushnikov
parent 9e39f5f8a6
commit 5ffbd0d221
3 changed files with 88 additions and 1 deletions

View File

@@ -147,6 +147,42 @@ class ElementHandle extends JSHandle {
clip: boundingBox
}, options));
}
/**
* @param {string} selector
* @return {!Promise<?ElementHandle>}
*/
async $(selector) {
const handle = await this.executionContext().evaluateHandle(
(element, selector) => element.querySelector(selector),
this, selector
);
const element = handle.asElement();
if (element)
return element;
await handle.dispose();
return null;
}
/**
* @param {string} selector
* @return {!Promise<!Array<!ElementHandle>>}
*/
async $$(selector) {
const arrayHandle = await this.executionContext().evaluateHandle(
(element, selector) => element.querySelectorAll(selector),
this, selector
);
const properties = await arrayHandle.getProperties();
await arrayHandle.dispose();
const result = [];
for (const property of properties.values()) {
const elementHandle = property.asElement();
if (elementHandle)
result.push(elementHandle);
}
return result;
}
}
module.exports = ElementHandle;