puppeteer/assets/js/85b4681f.21ac1406.js

2 lines
39 KiB
JavaScript
Raw Normal View History

/*! For license information please see 85b4681f.21ac1406.js.LICENSE.txt */
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[46087],{53517:(e,t,r)=>{r.r(t),r.d(t,{assets:()=>c,contentTitle:()=>d,default:()=>o,frontMatter:()=>n,metadata:()=>a,toc:()=>h});var s=r(85893),i=r(11151);const n={sidebar_label:"Page"},d="Page class",a={id:"api/puppeteer.page",title:"Page class",description:"Page provides methods to interact with a single tab or extension background page in the browser.",source:"@site/versioned_docs/version-21.6.0/api/puppeteer.page.md",sourceDirName:"api",slug:"/api/puppeteer.page",permalink:"/api/puppeteer.page",draft:!1,unlisted:!1,tags:[],version:"21.6.0",frontMatter:{sidebar_label:"Page"},sidebar:"api",previous:{title:"BrowserContext.waitForTarget",permalink:"/api/puppeteer.browsercontext.waitfortarget"},next:{title:"Page.$",permalink:"/api/puppeteer.page._"}},c={},h=[{value:"Signature:",id:"signature",level:4},{value:"Remarks",id:"remarks",level:2},{value:"Example 1",id:"example-1",level:2},{value:"Example 2",id:"example-2",level:2},{value:"Properties",id:"properties",level:2},{value:"Methods",id:"methods",level:2}];function p(e){const t={a:"a",admonition:"admonition",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,i.a)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.h1,{id:"page-class",children:"Page class"}),"\n",(0,s.jsxs)(t.p,{children:["Page provides methods to interact with a single tab or ",(0,s.jsx)(t.a,{href:"https://developer.chrome.com/extensions/background_pages",children:"extension background page"})," in the browser."]}),"\n",(0,s.jsx)(t.admonition,{type:"note",children:(0,s.jsx)(t.p,{children:"One Browser instance might have multiple Page instances."})}),"\n",(0,s.jsx)(t.h4,{id:"signature",children:"Signature:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-typescript",children:"export declare abstract class Page extends EventEmitter<PageEvents>\n"})}),"\n",(0,s.jsxs)(t.p,{children:[(0,s.jsx)(t.strong,{children:"Extends:"})," ",(0,s.jsx)(t.a,{href:"/api/puppeteer.eventemitter",children:"EventEmitter"}),"<",(0,s.jsx)(t.a,{href:"/api/puppeteer.pageevents",children:"PageEvents"}),">"]}),"\n",(0,s.jsx)(t.h2,{id:"remarks",children:"Remarks"}),"\n",(0,s.jsxs)(t.p,{children:["The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the ",(0,s.jsx)(t.code,{children:"Page"})," class."]}),"\n",(0,s.jsx)(t.h2,{id:"example-1",children:"Example 1"}),"\n",(0,s.jsx)(t.p,{children:"This example creates a page, navigates it to a URL, and then saves a screenshot:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ts",children:"import puppeteer from 'puppeteer';\n\n(async () => {\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.goto('https://example.com');\n await page.screenshot({path: 'screenshot.png'});\n await browser.close();\n})();\n"})}),"\n",(0,s.jsxs)(t.p,{children:["The Page class extends from Puppeteer's ",(0,s.jsx)(t.a,{href:"/api/puppeteer.eventemitter",children:"EventEmitter"})," class and will emit various events which are documented in the ",(0,s.jsx)(t.a,{href:"/api/puppeteer.pageevent",children:"PageEvent"})," enum."]}),"\n",(0,s.jsx)(t.h2,{id:"example-2",children:"Example 2"}),"\n",(0,s.jsxs)(t.p,{children:["This example logs a message for a single page ",(0,s.jsx)(t.code,{children:"load"})," event:"]}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ts",children:"page.once('load', () => console.log('Page loaded!'));\n"})}),"\n",(0,s.jsxs)(t.p,{children:["To unsubscribe from events use the ",(0,s.jsx)(t.a,{href:"/api/puppeteer.eventemitter.off",children:"EventEmitter.off()"})," method:"]}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-ts",children:"function logRequest(interceptedRequest) {\n console.log('A request was made:', interceptedRequest.url());\n}\npage.on('request', logRequest);\n// Sometime l