|
189 | 189 | - [class: ElementHandle](#class-elementhandle) |
190 | 190 | * [elementHandle.$(selector)](#elementhandleselector) |
191 | 191 | * [elementHandle.$$(selector)](#elementhandleselector) |
| 192 | + * [elementHandle.$eval(selector, pageFunction, ...args)](#elementhandleevalselector-pagefunction-args) |
192 | 193 | * [elementHandle.$x(expression)](#elementhandlexexpression) |
193 | 194 | * [elementHandle.asElement()](#elementhandleaselement) |
194 | 195 | * [elementHandle.boundingBox()](#elementhandleboundingbox) |
@@ -2230,6 +2231,23 @@ The method runs `element.querySelector` within the page. If no element matches t |
2230 | 2231 |
|
2231 | 2232 | The method runs `element.querySelectorAll` within the page. If no elements match the selector, the return value resolve to `[]`. |
2232 | 2233 |
|
| 2234 | +#### elementHandle.$eval(selector, pageFunction, ...args) |
| 2235 | +- `selector` <[string]> A [selector] to query page for |
| 2236 | +- `pageFunction` <[function]> Function to be evaluated in browser context |
| 2237 | +- `...args` <...[Serializable]|[JSHandle]> Arguments to pass to `pageFunction` |
| 2238 | +- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of `pageFunction` |
| 2239 | + |
| 2240 | +This method runs `document.querySelector` within the element and passes it as the first argument to `pageFunction`. If there's no element matching `selector`, the method throws an error. |
| 2241 | + |
| 2242 | +If `pageFunction` returns a [Promise], then `frame.$eval` would wait for the promise to resolve and return its value. |
| 2243 | + |
| 2244 | +Examples: |
| 2245 | +```js |
| 2246 | +const tweetHandle = await page.$('.tweet'); |
| 2247 | +expect(await tweetHandle.$eval('.like', node => node.innerText)).toBe('100'); |
| 2248 | +expect(await tweetHandle.$eval('.retweets', node => node.innerText)).toBe('10'); |
| 2249 | +``` |
| 2250 | + |
2233 | 2251 | #### elementHandle.$x(expression) |
2234 | 2252 | - `expression` <[string]> Expression to [evaluate](https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate). |
2235 | 2253 | - returns: <[Promise]<?[ElementHandle]>> Promise which resolves to ElementHandle pointing to the frame element. |
|
0 commit comments