puppeteer/next/api/puppeteer.page/index.html

75 lines
153 KiB
HTML
Raw Normal View History

<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-api/puppeteer.page" data-has-hydrated="false">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.3.2">
<title data-rh="true">Page class | Puppeteer</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://pptr.dev/next/api/puppeteer.page"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docsearch:counter" content="3"><meta data-rh="true" property="og:title" content="Page class | Puppeteer"><meta data-rh="true" name="description" content="Page provides methods to interact with a single tab or extension background page in the browser."><meta data-rh="true" property="og:description" content="Page provides methods to interact with a single tab or extension background page in the browser."><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://pptr.dev/next/api/puppeteer.page"><link data-rh="true" rel="alternate" href="https://pptr.dev/next/api/puppeteer.page" hreflang="en"><link data-rh="true" rel="alternate" href="https://pptr.dev/next/api/puppeteer.page" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://DVKY664LG7-dsn.algolia.net" crossorigin="anonymous"><link rel="search" type="application/opensearchdescription+xml" title="Puppeteer" href="/opensearch.xml">
<script src="/fix-location.js"></script><link rel="stylesheet" href="/assets/css/styles.67634aff.css">
<script src="/assets/js/runtime~main.131c3633.js" defer="defer"></script>
<script src="/assets/js/main.6786217b.js" defer="defer"></script>
</head>
<body class="navigation-with-keyboard">
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><div role="region" aria-label="Skip to main content"><a class="skipToContent_gu5v" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" alt="Puppeteer Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="https://user-images.githubusercontent.com/10379601/29446482-04f7036a-841f-11e7-9872-91d1fc2ea683.png" alt="Puppeteer Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Puppeteer</b></a><a class="navbar__item navbar__link" href="/next/category/introduction">Docs</a><a aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/next/api/puppeteer.puppeteernode">Puppeteer API</a><a class="navbar__item navbar__link" href="/next/browsers-api/">@puppeteer/browsers API</a></div><div class="navbar__items navbar__items--right"><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a class="navbar__link" aria-haspopup="true" aria-expanded="false" role="button" href="/next/">Next</a><ul class="dropdown__menu"><li><a aria-current="page" class="dropdown__link dropdown__link--active" href="/next/api/puppeteer.page">Next</a></li><li><a class="dropdown__link" href="/api/puppeteer.page">22.10.0</a></li><li><hr class="dropdown-separator"></li><li class="dropdown-archived-versions"><b>Archived versions</b></li><li><a href="https://github.com/puppeteer/puppeteer/blob/puppeteer-v22.9.0/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.9.0<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://github.com/puppeteer/puppeteer/blob/puppeteer-v22.8.2/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.8.2<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://github.com/puppeteer/puppeteer/blob/puppeteer-v22.8.1/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.8.1<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://github.com/puppeteer/puppeteer/blob/puppeteer-v22.8.0/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.8.0<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-1
<p>Page provides methods to interact with a single tab or <a href="https://developer.chrome.com/extensions/background_pages" target="_blank" rel="noopener noreferrer">extension background page</a> in the browser.</p>
<div class="theme-admonition theme-admonition-note admonition_LMjb alert alert--secondary"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_pGk6"><p>One Browser instance might have multiple Page instances.</p></div></div>
<h4 class="anchor anchorWithStickyNavbar_FNw8" id="signature">Signature:<a href="#signature" class="hash-link" aria-label="Direct link to Signature:" title="Direct link to Signature:"></a></h4>
<div class="language-typescript codeBlockContainer_aalF theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_MHx8"><pre tabindex="0" class="prism-code language-typescript codeBlock_zHgq thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_RjmQ"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">export</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">declare</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">abstract</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">class</span><span class="token plain"> </span><span class="token class-name">Page</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">extends</span><span class="token plain"> </span><span class="token class-name">EventEmitter</span><span class="token class-name operator" style="color:#393A34">&lt;</span><span class="token class-name">PageEvents</span><span class="token class-name operator" style="color:#393A34">&gt;</span><br></span></code></pre><div class="buttonGroup_Sd8_"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_LnQD" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_t3l1"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_IiZV"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<p><strong>Extends:</strong> <a href="/next/api/puppeteer.eventemitter">EventEmitter</a>&lt;<a href="/next/api/puppeteer.pageevents">PageEvents</a>&gt;</p>
<h2 class="anchor anchorWithStickyNavbar_FNw8" id="remarks">Remarks<a href="#remarks" class="hash-link" aria-label="Direct link to Remarks" title="Direct link to Remarks"></a></h2>
<p>The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the <code>Page</code> class.</p>
<h2 class="anchor anchorWithStickyNavbar_FNw8" id="example-1">Example 1<a href="#example-1" class="hash-link" aria-label="Direct link to Example 1" title="Direct link to Example 1"></a></h2>
<p>This example creates a page, navigates it to a URL, and then saves a screenshot:</p>
<div class="language-ts codeBlockContainer_aalF theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_MHx8"><pre tabindex="0" class="prism-code language-ts codeBlock_zHgq thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_RjmQ"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">import</span><span class="token plain"> puppeteer </span><span class="token keyword" style="color:#00009f">from</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">&#x27;puppeteer&#x27;</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">async</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword" style="color:#00009f">const</span><span class="token plain"> browser </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">await</span><span class="token plain"> puppeteer</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">launch</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword" style="color:#00009f">const</span><span class="token plain"> page </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token keyword" style="color:#00009f">await</span><span class="token plain"> browser</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">newPage</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword" style="color:#00009f">await</span><span class="token plain"> page</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">goto</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;https://example.com&#x27;</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword" style="color:#00009f">await</span><span class="token plain"> page</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">screenshot</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain">path</span><span class="token operator" style="color:#393A3
<p>The Page class extends from Puppeteer&#x27;s <a href="/next/api/puppeteer.eventemitter">EventEmitter</a> class and will emit various events which are documented in the <a href="/next/api/puppeteer.pageevent">PageEvent</a> enum.</p>
<h2 class="anchor anchorWithStickyNavbar_FNw8" id="example-2">Example 2<a href="#example-2" class="hash-link" aria-label="Direct link to Example 2" title="Direct link to Example 2"></a></h2>
<p>This example logs a message for a single page <code>load</code> event:</p>
<div class="language-ts codeBlockContainer_aalF theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_MHx8"><pre tabindex="0" class="prism-code language-ts codeBlock_zHgq thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_RjmQ"><span class="token-line" style="color:#393A34"><span class="token plain">page</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">once</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;load&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">=&gt;</span><span class="token plain"> </span><span class="token builtin">console</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">log</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;Page loaded!&#x27;</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup_Sd8_"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_LnQD" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_t3l1"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_IiZV"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<p>To unsubscribe from events use the <a href="/next/api/puppeteer.eventemitter.off">EventEmitter.off()</a> method:</p>
<div class="language-ts codeBlockContainer_aalF theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_MHx8"><pre tabindex="0" class="prism-code language-ts codeBlock_zHgq thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_RjmQ"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">function</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">logRequest</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">interceptedRequest</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token builtin">console</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">log</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;A request was made:&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> interceptedRequest</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">url</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">page</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">on</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;request&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> logRequest</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// Sometime later...</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">page</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">off</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;request&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> logRequest</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup_Sd8_"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_LnQD" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_t3l1"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_IiZV"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<h2 class="anchor anchorWithStickyNavbar_FNw8" id="properties">Properties<a href="#properties" class="hash-link" aria-label="Direct link to Properties" title="Direct link to Properties"></a></h2>
<table><thead><tr><th><p>Property</p></th><th><p>Modifiers</p></th><th><p>Type</p></th><th><p>Description</p></th></tr></thead><tbody><tr><td><span id="accessibility">accessibility</span></td><td><p><code>readonly</code></p></td><td><p><a href="/next/api/puppeteer.accessibility">Accessibility</a></p></td><td><p>The Accessibility class provides methods for inspecting the browser&#x27;s accessibility tree. The accessibility tree is used by assistive technology such as <a href="https://en.wikipedia.org/wiki/Screen_reader" target="_blank" rel="noopener noreferrer">screen readers</a> or <a href="https://en.wikipedia.org/wiki/Switch_access" target="_blank" rel="noopener noreferrer">switches</a>.</p><p><strong>Remarks:</strong></p><p>Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might have wildly different output.</p><p>Blink - Chrome&#x27;s rendering engine - has a concept of &quot;accessibility tree&quot;, which is then translated into different platform-specific APIs. Accessibility namespace gives users access to the Blink Accessibility Tree.</p><p>Most of the accessibility tree gets filtered out when converting from Blink AX Tree to Platform-specific AX-Tree or by assistive technologies themselves. By default, Puppeteer tries to approximate this filtering, exposing only the &quot;interesting&quot; nodes of the tree.</p><p>The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the <code>Accessibility</code> class.</p></td></tr><tr><td><span id="coverage">coverage</span></td><td><p><code>readonly</code></p></td><td><p><a href="/next/api/puppeteer.coverage">Coverage</a></p></td><td><p>The Coverage class provides methods to gather information about parts of JavaScript and CSS that were used by the page.</p><p><strong>Remarks:</strong></p><p>To output coverage in a form consumable by <a href="https://github.com/istanbuljs" target="_blank" rel="noopener noreferrer">Istanbul</a>, see <a href="https://github.com/istanbuljs/puppeteer-to-istanbul" target="_blank" rel="noopener noreferrer">puppeteer-to-istanbul</a>.</p></td></tr><tr><td><span id="keyboard">keyboard</span></td><td><p><code>readonly</code></p></td><td><p><a href="/next/api/puppeteer.keyboard">Keyboard</a></p></td><td><p>Keyboard provides an api for managing a virtual keyboard. The high level api is <a href="/next/api/puppeteer.keyboard.type">Keyboard.type()</a>, which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.</p><p><strong>Remarks:</strong></p><p>For finer control, you can use <a href="/next/api/puppeteer.keyboard.down">Keyboard.down()</a>, <a href="/next/api/puppeteer.keyboard.up">Keyboard.up()</a>, and <a href="/next/api/puppeteer.keyboard.sendcharacter">Keyboard.sendCharacter()</a> to manually fire events as if they were generated from a real keyboard.</p><p>On macOS, keyboard shortcuts like <code>⌘ A</code> -&gt; Select All do not work. See <a href="https://github.com/puppeteer/puppeteer/issues/1313" target="_blank" rel="noopener noreferrer">#1313</a>.</p><p>The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the <code>Keyboard</code> class.</p></td></tr><tr><td><span id="mouse">mouse</span></td><td><p><code>readonly</code></p></td><td><p><a href="/next/api/puppeteer.mouse">Mouse</a></p></td><td><p>The Mouse class operates in main-frame CSS pixels relative to the top-left corner of the viewport.</p><p><strong>Remarks:</strong></p><p>Every <code>page</code> object has its own Mouse, accessible with [<code>page.mouse</code>](#pagemouse).</p><p>The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the <code>Mouse</code> class.</p></td></tr><tr><td><span id="touchscreen">touchscreen</span></td><td><p><code>readonly</code></p></td><td><p><a href="/next/api/puppeteer.touchscreen"
<h2 class="anchor anchorWithStickyNavbar_FNw8" id="methods">Methods<a href="#methods" class="hash-link" aria-label="Direct link to Methods" title="Direct link to Methods"></a></h2>
<table><thead><tr><th><p>Method</p></th><th><p>Modifiers</p></th><th><p>Description</p></th></tr></thead><tbody><tr><td><span id="_"><a href="/next/api/puppeteer.page._">$(selector)</a></span></td><td></td><td><p>Finds the first element that matches the selector. If no element matches the selector, the return value resolves to <code>null</code>.</p><p><strong>Remarks:</strong></p><p>Shortcut for <a href="/next/api/puppeteer.frame._">Page.mainFrame().$(selector)</a>.</p></td></tr><tr><td><span id="__"><a href="/next/api/puppeteer.page.__">$$(selector)</a></span></td><td></td><td><p>Finds elements on the page that match the selector. If no elements match the selector, the return value resolves to <code>[]</code>.</p><p><strong>Remarks:</strong></p><p>Shortcut for <a href="/next/api/puppeteer.frame.__">Page.mainFrame().$$(selector)</a>.</p></td></tr><tr><td><span id="__eval"><a href="/next/api/puppeteer.page.__eval">$$eval(selector, pageFunction, args)</a></span></td><td></td><td><p>This method returns all elements matching the selector and passes the resulting array as the first argument to the <code>pageFunction</code>.</p><p><strong>Remarks:</strong></p><p>If <code>pageFunction</code> returns a promise <code>$$eval</code> will wait for the promise to resolve and then return its value.</p></td></tr><tr><td><span id="_eval"><a href="/next/api/puppeteer.page._eval">$eval(selector, pageFunction, args)</a></span></td><td></td><td><p>This method finds the first element within the page that matches the selector and passes the result as the first argument to the <code>pageFunction</code>.</p><p><strong>Remarks:</strong></p><p>If no element is found matching <code>selector</code>, the method will throw an error.</p><p>If <code>pageFunction</code> returns a promise <code>$eval</code> will wait for the promise to resolve and then return its value.</p></td></tr><tr><td><span id="addscripttag"><a href="/next/api/puppeteer.page.addscripttag">addScriptTag(options)</a></span></td><td></td><td><p>Adds a <code>&lt;script&gt;</code> tag into the page with the desired URL or content.</p><p><strong>Remarks:</strong></p><p>Shortcut for <a href="/next/api/puppeteer.frame.addscripttag">page.mainFrame().addScriptTag(options)</a>.</p></td></tr><tr><td><span id="addstyletag"><a href="/next/api/puppeteer.page.addstyletag">addStyleTag(options)</a></span></td><td></td><td><p>Adds a <code>&lt;link rel=&quot;stylesheet&quot;&gt;</code> tag into the page with the desired URL or a <code>&lt;style type=&quot;text/css&quot;&gt;</code> tag with the content.</p><p>Shortcut for <a href="/next/api/puppeteer.frame.addstyletag_1">page.mainFrame().addStyleTag(options)</a>.</p></td></tr><tr><td><span id="addstyletag"><a href="/next/api/puppeteer.page.addstyletag_1">addStyleTag(options)</a></span></td><td></td><td></td></tr><tr><td><span id="authenticate"><a href="/next/api/puppeteer.page.authenticate">authenticate(credentials)</a></span></td><td></td><td><p>Provide credentials for <code>HTTP authentication</code>.</p><div class="theme-admonition theme-admonition-note admonition_LMjb alert alert--secondary"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_pGk6"><p>Request interception will be turned on behind the scenes to implement authentication. This might affect performance.</p></div></div><p><strong>Remarks:</strong></p><p>To disable authentication, pass <code>null</code>.</p></td></tr><tr><td><span id="bringtofront"><a hre
<li>
<p>whenever the page is navigated</p>
</li>
<li>
<p>whenever the child frame is attached or navigated. In this case, the function is invoked in the context of the newly attached frame.</p>
</li>
</ul><p>The function is invoked after the document was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed <code>Math.random</code>.</p></td></tr><tr><td><span id="exposefunction"><a href="/next/api/puppeteer.page.exposefunction">exposeFunction(name, pptrFunction)</a></span></td><td></td><td><p>The method adds a function called <code>name</code> on the page&#x27;s <code>window</code> object. When called, the function executes <code>puppeteerFunction</code> in node.js and returns a <code>Promise</code> which resolves to the return value of <code>puppeteerFunction</code>.</p><p>If the puppeteerFunction returns a <code>Promise</code>, it will be awaited.</p><div class="theme-admonition theme-admonition-note admonition_LMjb alert alert--secondary"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_pGk6"><p>Functions installed via <code>page.exposeFunction</code> survive navigations.</p></div></div></td></tr><tr><td><span id="focus"><a href="/next/api/puppeteer.page.focus">focus(selector)</a></span></td><td></td><td><p>This method fetches an element with <code>selector</code> and focuses it. If there&#x27;s no element matching <code>selector</code>, the method throws an error.</p><p><strong>Remarks:</strong></p><p>Shortcut for <a href="/next/api/puppeteer.frame.focus">page.mainFrame().focus(selector)</a>.</p></td></tr><tr><td><span id="frames"><a href="/next/api/puppeteer.page.frames">frames()</a></span></td><td></td><td><p>An array of all frames attached to the page.</p></td></tr><tr><td><span id="getdefaulttimeout"><a href="/next/api/puppeteer.page.getdefaulttimeout">getDefaultTimeout()</a></span></td><td></td><td><p>Maximum time in milliseconds.</p></td></tr><tr><td><span id="goback"><a href="/next/api/puppeteer.page.goback">goBack(options)</a></span></td><td></td><td><p>This method navigate to the previous page in history.</p></td></tr><tr><td><span id="goforward"><a href="/next/api/puppeteer.page.goforward">goForward(options)</a></span></td><td></td><td><p>This method navigate to the next page in history.</p></td></tr><tr><td><span id="goto"><a href="/next/api/puppeteer.page.goto">goto(url, options)</a></span></td><td></td><td><p>Navigates the frame or page to the given <code>url</code>.</p><p><strong>Remarks:</strong></p><p>Navigation to <code>about:blank</code> or navigation to the same URL with a different hash will succeed and return <code>null</code>.</p><div class="theme-admonition theme-admonition-warning admonition_LMjb alert alert--warning"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>warning</div><div class="admonitionContent_pGk6"><p>Headless shell mode doesn&#x27;t support navigation to a PDF document. See the <a href="https://crbug.com/761295" target="_blank" rel="noopener noreferrer">upstream issue</a>.</p></div></div><p>In headless shell, this method will not throw an error when any valid HTTP status code is returned by the remote server, including 404 &quot;Not Found&quot; and 500 &quot;Internal Server Error&quot;. The status code for such responses can be retrieved by ca
<li>
<p><a href="/next/api/puppeteer.page.goback">page.goBack(options)</a></p>
</li>
<li>
<p><a href="/next/api/puppeteer.page.goforward">page.goForward(options)</a></p>
</li>
<li>
<p><a href="/next/api/puppeteer.page.goto">page.goto(url,options)</a></p>
</li>
<li>
<p><a href="/next/api/puppeteer.page.reload">page.reload(options)</a></p>
</li>
<li>
<p><a href="/next/api/puppeteer.page.setcontent">page.setContent(html,options)</a></p>
</li>
<li>
<p><a href="/next/api/puppeteer.page.waitfornavigation">page.waitForNavigation(options)</a></p>
</li>
</ul></td></tr><tr><td><span id="setdefaulttimeout"><a href="/next/api/puppeteer.page.setdefaulttimeout">setDefaultTimeout(timeout)</a></span></td><td></td><td></td></tr><tr><td><span id="setdraginterception"><a href="/next/api/puppeteer.page.setdraginterception">setDragInterception(enabled)</a></span></td><td><p><code>deprecated</code></p></td><td><p><strong>Deprecated:</strong></p><p>We no longer support intercepting drag payloads. Use the new drag APIs found on <a href="/next/api/puppeteer.elementhandle">ElementHandle</a> to drag (or just use the <a href="/next/api/puppeteer.page#mouse">Page.mouse</a>).</p></td></tr><tr><td><span id="setextrahttpheaders"><a href="/next/api/puppeteer.page.setextrahttpheaders">setExtraHTTPHeaders(headers)</a></span></td><td></td><td><p>The extra HTTP headers will be sent with every request the page initiates.</p><div class="theme-admonition theme-admonition-tip admonition_LMjb alert alert--success"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><div class="admonitionContent_pGk6"><p>All HTTP header names are lowercased. (HTTP headers are case-insensitive, so this shouldnt impact your server code.)</p></div></div><div class="theme-admonition theme-admonition-note admonition_LMjb alert alert--secondary"><div class="admonitionHeading_GGQ4"><span class="admonitionIcon_ifdW"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_pGk6"><p>page.setExtraHTTPHeaders does not guarantee the order of headers in the outgoing requests.</p></div></div></td></tr><tr><td><span id="setgeolocation"><a href="/next/api/puppeteer.page.setgeolocation">setGeolocation(options)</a></span></td><td></td><td><p>Sets the page&#x27;s geolocation.</p><p><strong>Remarks:</strong></p><p>Consider using <a href="/next/api/puppeteer.browsercontext.overridepermissions">BrowserContext.overridePermissions()</a> to grant permissions for the page to read its geolocation.</p></td></tr><tr><td><span id="setjavascriptenabled"><a href="/next/api/puppeteer.page.setjavascriptenabled">setJavaScriptEnabled(enabled)</a></span></td><td></td><td><p><strong>Remarks:</strong></p><p>NOTE: changing this value won&#x27;t affect scripts that have already been run. It will take full effect on the next navigation.</p></td></tr><tr><td><span id="setofflinemode"><a href="/next/api/puppeteer.page.setofflinemode">setOfflineMode(enabled)</a></span></td><td></td><td><p>Sets the network connection to offline.</p><p>It does not change the parameters used in <a href="/next/api/puppeteer.page.emulatenetworkconditions">Page.emulateNetworkConditions()</a></p></td></tr><tr><td><span id="setrequestinterception"><a href="/next/api/puppeteer.page.setrequestinterception">setRequestInterception(value)</a></span></td><td></td><td><p>Activating request interception enables <a href="/next/api/puppeteer.httprequest.abort">HTTPRequest.abort()</a>, <a href="/next/api/puppeteer.httprequest.continue">HTTPRequest.continue()</a> and <a href="/next/api/puppeteer.
<li><code>timeout</code>: Maximum wait time in milliseconds, defaults to <code>30</code> seconds, pass <code>0</code> to disable the timeout. The default value can be changed by using the <a href="/next/api/puppeteer.page.setdefaulttimeout">Page.setDefaultTimeout()</a> method.</li>
</ul></td></tr><tr><td><span id="waitforresponse"><a href="/next/api/puppeteer.page.waitforresponse">waitForResponse(urlOrPredicate, options)</a></span></td><td></td><td><p><strong>Remarks:</strong></p><p>Optional Parameter have:</p><ul>
<li><code>timeout</code>: Maximum wait time in milliseconds, defaults to <code>30</code> seconds, pass <code>0</code> to disable the timeout. The default value can be changed by using the <a href="/next/api/puppeteer.page.setdefaulttimeout">Page.setDefaultTimeout()</a> method.</li>
</ul></td></tr><tr><td><span id="waitforselector"><a href="/next/api/puppeteer.page.waitforselector">waitForSelector(selector, options)</a></span></td><td></td><td><p>Wait for the <code>selector</code> to appear in page. If at the moment of calling the method the <code>selector</code> already exists, the method will return immediately. If the <code>selector</code> doesn&#x27;t appear after the <code>timeout</code> milliseconds of waiting, the function will throw.</p><p><strong>Remarks:</strong></p><p>The optional Parameter in Arguments <code>options</code> are:</p><ul>
<li>
<p><code>visible</code>: A boolean wait for element to be present in DOM and to be visible, i.e. to not have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. Defaults to <code>false</code>.</p>
</li>
<li>
<p><code>hidden</code>: Wait for element to not be found in the DOM or to be hidden, i.e. have <code>display: none</code> or <code>visibility: hidden</code> CSS properties. Defaults to <code>false</code>.</p>
</li>
<li>
<p><code>timeout</code>: maximum time to wait for in milliseconds. Defaults to <code>30000</code> (30 seconds). Pass <code>0</code> to disable timeout. The default value can be changed by using the <a href="/next/api/puppeteer.page.setdefaulttimeout">Page.setDefaultTimeout()</a> method.</p>
</li>
</ul></td></tr><tr><td><span id="workers"><a href="/next/api/puppeteer.page.workers">workers()</a></span></td><td></td><td><p>All of the dedicated <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API" target="_blank" rel="noopener noreferrer">WebWorkers</a> associated with the page.</p><p><strong>Remarks:</strong></p><p>This does not contain ServiceWorkers</p></td></tr></tbody></table></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/next/api/puppeteer.browsercontext.waitfortarget"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">BrowserContext.waitForTarget</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/next/api/puppeteer.page._"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Page.$</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_IS5x thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#remarks" class="table-of-contents__link toc-highlight">Remarks</a></li><li><a href="#example-1" class="table-of-contents__link toc-highlight">Example 1</a></li><li><a href="#example-2" class="table-of-contents__link toc-highlight">Example 2</a></li><li><a href="#properties" class="table-of-contents__link toc-highlight">Properties</a></li><li><a href="#methods" class="table-of-contents__link toc-highlight">Methods</a></li></ul></div></div></div></div></main></div></div></div><footer class="footer footer--dark"><div class="container container-fluid"><div class="row footer__links"><div class="col footer__col"><div class="footer__title">Community</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://stackoverflow.com/questions/tagged/puppeteer" target="_blank" rel="noopener noreferrer" class="footer__link-item">Stack Overflow<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://twitter.com/chromedevtools" target="_blank" rel="noopener noreferrer" class="footer__link-item">Twitter<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://goo.gle/devtools-youtube" target="_blank" rel="noopener noreferrer" class="footer__link-item">YouTube<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_T11m"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div></div><div class="footer__bottom text--center"><div class="footer__copyright">Copyright © 2024 Google, Inc.</div></div></div></footer></div>
</body>
</html>