/*! For license information please see 71d5c8d7.34940300.js.LICENSE.txt */ "use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[79204],{93900:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>l,default:()=>p,frontMatter:()=>s,metadata:()=>i,toc:()=>d});var a=t(85893),r=t(11151);const s={sidebar_label:"Page.evaluate"},l="Page.evaluate() method",i={id:"api/puppeteer.page.evaluate",title:"Page.evaluate() method",description:"Evaluates a function in the page's context and returns the result.",source:"@site/../docs/api/puppeteer.page.evaluate.md",sourceDirName:"api",slug:"/api/puppeteer.page.evaluate",permalink:"/next/api/puppeteer.page.evaluate",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{sidebar_label:"Page.evaluate"},sidebar:"api",previous:{title:"Page.emulateVisionDeficiency",permalink:"/next/api/puppeteer.page.emulatevisiondeficiency"},next:{title:"Page.evaluateHandle",permalink:"/next/api/puppeteer.page.evaluatehandle"}},c={},d=[{value:"Signature:",id:"signature",level:4},{value:"Parameters",id:"parameters",level:2},{value:"Example 1",id:"example-1",level:2},{value:"Example 2",id:"example-2",level:2},{value:"Example 3",id:"example-3",level:2}];function o(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",h4:"h4",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",...(0,r.a)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.h1,{id:"pageevaluate-method",children:"Page.evaluate() method"}),"\n",(0,a.jsx)(n.p,{children:"Evaluates a function in the page's context and returns the result."}),"\n",(0,a.jsxs)(n.p,{children:["If the function passed to ",(0,a.jsx)(n.code,{children:"page.evaluate"})," returns a Promise, the function will wait for the promise to resolve and return its value."]}),"\n",(0,a.jsx)(n.h4,{id:"signature",children:"Signature:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-typescript",children:"class Page {\n evaluate<\n Params extends unknown[],\n Func extends EvaluateFunc = EvaluateFunc,\n >(\n pageFunction: Func | string,\n ...args: Params\n ): Promise>>;\n}\n"})}),"\n",(0,a.jsx)(n.h2,{id:"parameters",children:"Parameters"}),"\n",(0,a.jsxs)(n.table,{children:[(0,a.jsx)(n.thead,{children:(0,a.jsxs)(n.tr,{children:[(0,a.jsx)(n.th,{children:"Parameter"}),(0,a.jsx)(n.th,{children:"Type"}),(0,a.jsx)(n.th,{children:"Description"})]})}),(0,a.jsxs)(n.tbody,{children:[(0,a.jsxs)(n.tr,{children:[(0,a.jsx)(n.td,{children:"pageFunction"}),(0,a.jsx)(n.td,{children:"Func | string"}),(0,a.jsx)(n.td,{children:"a function that is run within the page"})]}),(0,a.jsxs)(n.tr,{children:[(0,a.jsx)(n.td,{children:"args"}),(0,a.jsx)(n.td,{children:"Params"}),(0,a.jsx)(n.td,{children:"arguments to be passed to the pageFunction"})]})]})]}),"\n",(0,a.jsx)(n.p,{children:(0,a.jsx)(n.strong,{children:"Returns:"})}),"\n",(0,a.jsx)(n.p,{children:"Promise>>"}),"\n",(0,a.jsxs)(n.p,{children:["the return value of ",(0,a.jsx)(n.code,{children:"pageFunction"}),"."]}),"\n",(0,a.jsx)(n.h2,{id:"example-1",children:"Example 1"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-ts",children:'const result = await frame.evaluate(() => {\n return Promise.resolve(8 * 7);\n});\nconsole.log(result); // prints "56"\n'})}),"\n",(0,a.jsx)(n.p,{children:"You can pass a string instead of a function (although functions are recommended as they are easier to debug and use with TypeScript):"}),"\n",(0,a.jsx)(n.h2,{id:"example-2",children:"Example 2"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-ts",children:"const aHandle = await page.evaluate('1 + 2');\n"})}),"\n",(0,a.jsxs)(n.p,{children:["To get the best TypeScript experience, you should pass in as the generic the type of ",(0,a.jsx)(n.code,{children:"pageFunction"}),":"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-ts",children:"const aHandle = await page.evaluate(() => 2);\n"})}),"\n",(0,a.jsx)(n.h2,{id:"example-3",children:"Example 3"}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.a,{href:"/next/api/puppeteer.elementhandle",children:"ElementHandle"})," instances (including ",(0,a.jsx)(n.a,{href:"/next/api/puppeteer.jshandle",children:"JSHandle"}),"s) can be passed as arguments to the ",(0,a.jsx)(n.code,{children:"pageFunction"}),":"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-ts",children:"const bodyHandle = await page.$('body');\nconst html = await page.evaluate(body => body.innerHTML, bodyHandle);\nawait bodyHandle.dispose();\n"})})]})}function p(e={}){const{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(o,{...e})}):o(e)}},75251:(e,n,t)=>{var a=t(67294),r=Symbol.for("react.element"),s=Symbol.for("react.fragment"),l=Object.prototype.hasOwnProperty,i=a.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,c={key:!0,ref:!0,__self:!0,__source:!0};function d(e,n,t){var a,s={},d=null,o=null;for(a in void 0!==t&&(d=""+t),void 0!==n.key&&(d=""+n.key),void 0!==n.ref&&(o=n.ref),n)l.call(n,a)&&!c.hasOwnProperty(a)&&(s[a]=n[a]);if(e&&e.defaultProps)for(a in n=e.defaultProps)void 0===s[a]&&(s[a]=n[a]);return{$$typeof:r,type:e,key:d,ref:o,props:s,_owner:i.current}}n.Fragment=s,n.jsx=d,n.jsxs=d},85893:(e,n,t)=>{e.exports=t(75251)},11151:(e,n,t)=>{t.d(n,{Z:()=>i,a:()=>l});var a=t(67294);const r={},s=a.createContext(r);function l(e){const n=a.useContext(s);return a.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:l(e.components),a.createElement(s.Provider,{value:n},e.children)}}}]);