puppeteer/new-docs/puppeteer.page.__eval.md

50 lines
1.6 KiB
Markdown
Raw Normal View History

<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [puppeteer](./puppeteer.md) &gt; [Page](./puppeteer.page.md) &gt; [$$eval](./puppeteer.page.__eval.md)
## Page.$$eval() method
This method runs `Array.from(document.querySelectorAll(selector))` within the page and passes the result as the first argument to the `pageFunction`<!-- -->.
<b>Signature:</b>
```typescript
$$eval<ReturnType extends any>(selector: string, pageFunction: EvaluateFn | string, ...args: SerializableOrJSHandle[]): Promise<ReturnType>;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| selector | string | the [selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) to query for |
| pageFunction | [EvaluateFn](./puppeteer.evaluatefn.md) \| string | the function to be evaluated in the page context. Will be passed the result of <code>Array.from(document.querySelectorAll(selector))</code> as its first argument. |
| args | [SerializableOrJSHandle](./puppeteer.serializableorjshandle.md)<!-- -->\[\] | any additional arguments to pass through to <code>pageFunction</code>. |
<b>Returns:</b>
Promise&lt;ReturnType&gt;
The result of calling `pageFunction`<!-- -->.
## Remarks
If `pageFunction` returns a promise `$$eval` will wait for the promise to resolve and then return its value.
## Example 1
```js
const divCount = await page.$$eval('div', divs => divs.length);
```
## Example 2
```js
const options = await page.$$eval(
'div > span.options', options => options.map(option => option.textContent));
```