"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[78530],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>g});var a=n(67294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function i(e){for(var t=1;t=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var p=a.createContext({}),u=function(e){var t=a.useContext(p),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},c=function(e){var t=u(e.components);return a.createElement(p.Provider,{value:t},e.children)},s={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,o=e.originalType,p=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),m=u(n),g=r,d=m["".concat(p,".").concat(g)]||m[g]||s[g]||o;return n?a.createElement(d,i(i({ref:t},c),{},{components:n})):a.createElement(d,i({ref:t},c))}));function g(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var o=n.length,i=new Array(o);i[0]=m;var l={};for(var p in t)hasOwnProperty.call(t,p)&&(l[p]=t[p]);l.originalType=e,l.mdxType="string"==typeof e?e:r,i[1]=l;for(var u=2;u{n.r(t),n.d(t,{assets:()=>p,contentTitle:()=>i,default:()=>s,frontMatter:()=>o,metadata:()=>l,toc:()=>u});var a=n(87462),r=(n(67294),n(3905));const o={sidebar_label:"Page.waitForFunction"},i="Page.waitForFunction() method",l={unversionedId:"api/puppeteer.page.waitforfunction",id:"api/puppeteer.page.waitforfunction",title:"Page.waitForFunction() method",description:"Waits for a function to finish evaluating in the page's context.",source:"@site/../docs/api/puppeteer.page.waitforfunction.md",sourceDirName:"api",slug:"/api/puppeteer.page.waitforfunction",permalink:"/next/api/puppeteer.page.waitforfunction",draft:!1,tags:[],version:"current",frontMatter:{sidebar_label:"Page.waitForFunction"},sidebar:"sidebar",previous:{title:"Page.waitForFrame",permalink:"/next/api/puppeteer.page.waitforframe"},next:{title:"Page.waitForNavigation",permalink:"/next/api/puppeteer.page.waitfornavigation"}},p={},u=[{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}],c={toc:u};function s(e){let{components:t,...n}=e;return(0,r.kt)("wrapper",(0,a.Z)({},c,n,{components:t,mdxType:"MDXLayout"}),(0,r.kt)("h1",{id:"pagewaitforfunction-method"},"Page.waitForFunction() method"),(0,r.kt)("p",null,"Waits for a function to finish evaluating in the page's context."),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Signature:")),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-typescript"},"class Page {waitForFunction = EvaluateFunc>(pageFunction: Func | string, options?: {\n timeout?: number;\n polling?: string | number;\n }, ...args: Params): Promise>>>;}\n")),(0,r.kt)("h2",{id:"parameters"},"Parameters"),(0,r.kt)("table",null,(0,r.kt)("thead",{parentName:"table"},(0,r.kt)("tr",{parentName:"thead"},(0,r.kt)("th",{parentName:"tr",align:null},"Parameter"),(0,r.kt)("th",{parentName:"tr",align:null},"Type"),(0,r.kt)("th",{parentName:"tr",align:null},"Description"))),(0,r.kt)("tbody",{parentName:"table"},(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"pageFunction"),(0,r.kt)("td",{parentName:"tr",align:null},"Func ","|"," string"),(0,r.kt)("td",{parentName:"tr",align:null},"Function to be evaluated in browser context")),(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"options"),(0,r.kt)("td",{parentName:"tr",align:null},"{ timeout?: number; polling?: string ","|"," number; }"),(0,r.kt)("td",{parentName:"tr",align:null},(0,r.kt)("i",null,"(Optional)")," Optional waiting parameters - ",(0,r.kt)("code",null,"polling")," - An interval at which the ",(0,r.kt)("code",null,"pageFunction")," is executed, defaults to ",(0,r.kt)("code",null,"raf"),". If ",(0,r.kt)("code",null,"polling")," is a number, then it is treated as an interval in milliseconds at which the function would be executed. If polling is a string, then it can be one of the following values: - ",(0,r.kt)("code",null,"raf")," - to constantly execute ",(0,r.kt)("code",null,"pageFunction")," in ",(0,r.kt)("code",null,"requestAnimationFrame")," callback. This is the tightest polling mode which is suitable to observe styling changes. - ",(0,r.kt)("code",null,"mutation"),"- to execute pageFunction on every DOM mutation. - ",(0,r.kt)("code",null,"timeout")," - maximum time to wait for in milliseconds. Defaults to ",(0,r.kt)("code",null,"30000")," (30 seconds). Pass ",(0,r.kt)("code",null,"0")," to disable timeout. The default value can be changed by using the ",(0,r.kt)("a",{parentName:"td",href:"/next/api/puppeteer.page.setdefaulttimeout"},"Page.setDefaultTimeout()")," method.")),(0,r.kt)("tr",{parentName:"tbody"},(0,r.kt)("td",{parentName:"tr",align:null},"args"),(0,r.kt)("td",{parentName:"tr",align:null},"Params"),(0,r.kt)("td",{parentName:"tr",align:null},"Arguments to pass to ",(0,r.kt)("code",null,"pageFunction"))))),(0,r.kt)("p",null,(0,r.kt)("strong",{parentName:"p"},"Returns:")),(0,r.kt)("p",null,"Promise","<",(0,r.kt)("a",{parentName:"p",href:"/next/api/puppeteer.handlefor"},"HandleFor"),"<","Awaited","<","ReturnType","<","Func",">",">",">",">"),(0,r.kt)("p",null,"A ",(0,r.kt)("inlineCode",{parentName:"p"},"Promise")," which resolves to a JSHandle/ElementHandle of the the ",(0,r.kt)("inlineCode",{parentName:"p"},"pageFunction"),"'s return value."),(0,r.kt)("h2",{id:"example-1"},"Example 1"),(0,r.kt)("p",null,"The ",(0,r.kt)("a",{parentName:"p",href:"/next/api/puppeteer.page.waitforfunction"},"Page.waitForFunction()")," can be used to observe viewport size change:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"const puppeteer = require('puppeteer');\n(async () => {\nconst browser = await puppeteer.launch();\nconst page = await browser.newPage();\nconst watchDog = page.waitForFunction('window.innerWidth < 100');\nawait page.setViewport({ width: 50, height: 50 });\nawait watchDog;\nawait browser.close();\n})();\n")),(0,r.kt)("h2",{id:"example-2"},"Example 2"),(0,r.kt)("p",null,"To pass arguments from node.js to the predicate of ",(0,r.kt)("a",{parentName:"p",href:"/next/api/puppeteer.page.waitforfunction"},"Page.waitForFunction()")," function:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"const selector = '.foo';\nawait page.waitForFunction(\n(selector) => !!document.querySelector(selector),\n{},\nselector\n);\n")),(0,r.kt)("h2",{id:"example-3"},"Example 3"),(0,r.kt)("p",null,"The predicate of ",(0,r.kt)("a",{parentName:"p",href:"/next/api/puppeteer.page.waitforfunction"},"Page.waitForFunction()")," can be asynchronous too:"),(0,r.kt)("pre",null,(0,r.kt)("code",{parentName:"pre",className:"language-ts"},"const username = 'github-username';\nawait page.waitForFunction(\nasync (username) => {\nconst githubResponse = await fetch(\n `https://api.github.com/users/${username}`\n);\nconst githubUser = await githubResponse.json();\n// show the avatar\nconst img = document.createElement('img');\nimg.src = githubUser.avatar_url;\n// wait 3 seconds\nawait new Promise((resolve, reject) => setTimeout(resolve, 3000));\nimg.remove();\n},\n{},\nusername\n);\n")))}s.isMDXComponent=!0}}]);