💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# class:executioncontext ### class: ExecutionContext v0.9.0 该类表示一个 JavaScript 执行的上下文。 [Page](#?product=Puppeteer&version=v1.11.0&show=api-class-page "Page") 可能有许多执行上下文: - 每个 [frame](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) 都有 "默认" 的执行上下文,它始终在将帧附加到 DOM 后创建。该上下文由 [`frame.executionContext()`](#?product=Puppeteer&version=v1.11.0&show=api-frameexecutioncontext) 方法返回。 - [Extensions](https://developer.chrome.com/extensions) 的内容脚本创建了其他执行上下文。 除了页面,执行上下文可以在 [workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) 中找到。 #### Methods - [executionContext.evaluate(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatepagefunction-args)v0.9.0 - [executionContext.evaluateHandle(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatehandlepagefunction-args)v0.9.0 - [executionContext.frame()](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextframe)v0.9.0 - [executionContext.queryObjects(prototypeHandle)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextqueryobjectsprototypehandle)v0.9.0 ### Methods #### executionContext.evaluate(pageFunction, ...args)v0.9.0 - `pageFunction` <[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Function to be evaluated in `executionContext` - `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> Arguments to pass to `pageFunction` - returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")>> Promise which resolves to the return value of `pageFunction` 如果传递给 `executionContext.evaluate` 的函数返回一个[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么 `executionContext.evaluate` 将等待承诺解析并返回它的值。 ``` const executionContext = await page.mainFrame().executionContext();const result = await executionContext.evaluate(() => Promise.resolve(8 * 7));console.log(result); // 输出 "56" ``` 入参可以是一个字符串,但不能是函数。 ``` console.log(await executionContext.evaluate('1 + 2')); // 输出 "3" ``` [JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle") 实例可以作为参数传递给 `executionContext.evaluate`: ``` const oneHandle = await executionContext.evaluateHandle(() => 1);const twoHandle = await executionContext.evaluateHandle(() => 2);const result = await executionContext.evaluate((a, b) => a + b, oneHandle, twoHandle);await oneHandle.dispose();await twoHandle.dispose();console.log(result); // 输出 '3' ``` #### executionContext.evaluateHandle(pageFunction, ...args)v0.9.0 - `pageFunction` <[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 函数在 `executionContext` 中被运行 - `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 传递给 `pageFunction` 的参数 - returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")>> Promise which resolves to the return value of `pageFunction` as in-page object (JSHandle) `executionContext.evaluate` 和 `executionContext.evaluateHandle` 唯一的区别在于`executionContext.evaluateHandle` 会返回页内对象(JSHandle)。 如果传递给 `executionContext.evaluateHandle` 的函数返回一个 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么`executionContext.evaluateHandle`将等待承诺解析并返回它的值。 ``` const context = await page.mainFrame().executionContext();const aHandle = await context.evaluateHandle(() => Promise.resolve(self));aHandle; // 处理全局对象 ``` 入参可以是一个字符串,但不能是函数。 ``` const aHandle = await context.evaluateHandle('1 + 2'); // 处理'3'对象 ``` [JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle") 实例可以作为参数传递给 `executionContext.evaluateHandle`: ``` const aHandle = await context.evaluateHandle(() => document.body);const resultHandle = await context.evaluateHandle(body => body.innerHTML, aHandle);console.log(await resultHandle.jsonValue()); // 输出 body 的 innerHTMLawait aHandle.dispose();await resultHandle.dispose(); ``` #### executionContext.frame()v0.9.0 - returns: <?[Frame](#?product=Puppeteer&version=v1.11.0&show=api-class-frame "Frame")> 与此执行上下文相关的框架。 > **注意** 并非每个执行的上下文都与框架有关系。 例如,workers 和扩展程序具有与框架无关的执行上下文。 #### executionContext.queryObjects(prototypeHandle)v0.9.0 - `prototypeHandle` <[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 对象原型的句柄 - returns: <[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 这个原型的一个对象数组的句柄 该方法重复查找 JavaScript 堆,找到具有给定原型的所有对象。 ``` // 创建一个 Map 对象await page.evaluate(() => window.map = new Map());// 获取 Map 对象原型的句柄const mapPrototype = await page.evaluateHandle(() => Map.prototype);// 将所有映射实例查询到一个数组中const mapInstances = await page.queryObjects(mapPrototype);// 计算堆中映射对象的数量const count = await page.evaluate(maps => maps.length, mapInstances);await mapInstances.dispose();await mapPrototype.dispose(); ``` ![](images/pptr.png) puppeteer.js中文网|class:executioncontext puppeteer.js中文文档, puppeteer chrome, puppeteer firefox, puppeteer api 中文文档 puppeteer.js中文网包含了Puppeteer中文文档,最新资讯,应用案例等。Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome。