puppeteer/guides/debugging/index.html

121 lines
101 KiB
HTML
Raw Normal View History

<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-22.7.1 docs-doc-page docs-doc-id-guides/debugging" data-has-hydrated="false">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.2.1">
<title data-rh="true">Debugging | 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/guides/debugging"><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="2"><meta data-rh="true" property="og:title" content="Debugging | Puppeteer"><meta data-rh="true" name="description" content="Debugging with Puppeteer can be an arduous task. There is no single method for"><meta data-rh="true" property="og:description" content="Debugging with Puppeteer can be an arduous task. There is no single method for"><link data-rh="true" rel="icon" href="/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://pptr.dev/guides/debugging"><link data-rh="true" rel="alternate" href="https://pptr.dev/guides/debugging" hreflang="en"><link data-rh="true" rel="alternate" href="https://pptr.dev/guides/debugging" 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.76d942d0.css">
<script src="/assets/js/runtime~main.0c19c266.js" defer="defer"></script>
<script src="/assets/js/main.5a5cf32e.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_fXgn" 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 aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/category/introduction">Docs</a><a class="navbar__item navbar__link" href="/api/puppeteer.puppeteernode">Puppeteer API</a><a class="navbar__item navbar__link" href="/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="/">22.7.1</a><ul class="dropdown__menu"><li><a class="dropdown__link" href="/next/guides/debugging">Next</a></li><li><a aria-current="page" class="dropdown__link dropdown__link--active" href="/guides/debugging">22.7.1</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.7.0/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.7.0<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><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.6.5/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.6.5<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><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.6.4/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.6.4<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><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.6.3/docs/api/index.md" target="_blank" rel="noopener noreferrer" class="dropdown__link">22.6.3<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.
<p>Debugging with Puppeteer can be an arduous task. There is no <em>single</em> method for
debugging all possible issues since Puppeteer touches many distinct components
of a browser such as network requests and Web APIs. On a high note, Puppeteer
provides <em>several</em> methods for debugging which hopefully does cover all possible
issues.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="background">Background<a href="#background" class="hash-link" aria-label="Direct link to Background" title="Direct link to Background"></a></h2>
<p>In general, there are two possible sources of an issue: Code running on Node.js
(which we call <em>server code</em>), and
<a href="/api/puppeteer.page.evaluate">code running in the browser</a>
(which we call <em>client code</em>). There is also a third possible source being the
browser itself (which we call <em>internal code</em> or <em>browser code</em>), but if you suspect this is the
source <strong>after attempting the methods below</strong>, we suggest
<a href="https://github.com/puppeteer/puppeteer/issues" target="_blank" rel="noopener noreferrer">searching existing issues</a>
before
<a href="https://github.com/puppeteer/puppeteer/issues/new/choose" target="_blank" rel="noopener noreferrer">filing an issue</a>.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="debugging-methods-for-all-situations">Debugging methods for all situations<a href="#debugging-methods-for-all-situations" class="hash-link" aria-label="Direct link to Debugging methods for all situations" title="Direct link to Debugging methods for all situations"></a></h2>
<p>These methods can be used to debug any situation. These should be used as a
quick sanity check before diving into more complex methods.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="turn-off-headless">Turn off <a href="/api/puppeteer.browserlaunchargumentoptions"><code>headless</code></a><a href="#turn-off-headless" class="hash-link" aria-label="Direct link to turn-off-headless" title="Direct link to turn-off-headless"></a></h3>
<p>Sometimes it&#x27;s useful to see what the browser is displaying. Instead of
launching in
<a href="/api/puppeteer.browserlaunchargumentoptions"><code>headless</code></a> mode,
launch a full version of the browser with
<a href="/api/puppeteer.browserlaunchargumentoptions"><code>headless</code></a> set to
<code>false</code>:</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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 plain">headless</span><span class="token operator" style="color:#393A34">:</span><span class="token plain"> </span><span class="token boolean" style="color:#36acaa">false</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__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="puppeteer-slow-mo">Puppeteer &quot;slow-mo&quot;<a href="#puppeteer-slow-mo" class="hash-link" aria-label="Direct link to Puppeteer &quot;slow-mo&quot;" title="Direct link to Puppeteer &quot;slow-mo&quot;"></a></h3>
<p>The <a href="/api/puppeteer.browserconnectoptions"><code>slowMo</code></a> option slows down
Puppeteer operations by a specified amount of milliseconds. It&#x27;s another way to
help see what&#x27;s going on.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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 plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> headless</span><span class="token operator" style="color:#393A34">:</span><span class="token plain"> </span><span class="token boolean" style="color:#36acaa">false</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"> slowMo</span><span class="token operator" style="color:#393A34">:</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">250</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// slow down by 250ms</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 punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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_LWe7" id="debugging-methods-for-client-code">Debugging methods for client code<a href="#debugging-methods-for-client-code" class="hash-link" aria-label="Direct link to Debugging methods for client code" title="Direct link to Debugging methods for client code"></a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="capture-console-output">Capture <code>console.*</code> output<a href="#capture-console-output" class="hash-link" aria-label="Direct link to capture-console-output" title="Direct link to capture-console-output"></a></h3>
<p>Since client code runs in the browser, doing <code>console.*</code> in client code will not
directly log to Node.js. However, you can <a href="/api/puppeteer.page">listen (page.on)</a> for
the <a href="/api/puppeteer.pageevents"><code>console</code></a> event which returns a
payload with the logged text.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><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;console&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> msg </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 LOG:&#x27;</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> msg</span><span class="token punctuation" style="color:#393A34">.</span><span class="token function" style="color:#d73a49">text</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 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 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">evaluate</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><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 template-string template-punctuation string" style="color:#e3116c">`</span><span class="token template-string string" style="color:#e3116c">url is </span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:#393A34">${</span><span class="token template-string interpolation">location</span><span class="token template-string interpolation punctuation" style="color:#393A34">.</span><span class="token template-string interpolation">href</span><span class="token template-string interpolation interpolation-punctuation punctuation" style="color:#393A34">}</span><span class="token template-string template-punctuation string" style="color:#e3116c">`</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__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="use-the-debugger-in-the-browser">Use the debugger in the browser<a href="#use-the-debugger-in-the-browser" class="hash-link" aria-label="Direct link to Use the debugger in the browser" title="Direct link to Use the debugger in the browser"></a></h3>
<ol>
<li>
<p>Set <a href="/api/puppeteer.browserlaunchargumentoptions"><code>devtools</code></a> to
<code>true</code> when launching Puppeteer:</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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 plain">devtools</span><span class="token operator" style="color:#393A34">:</span><span class="token plain"> </span><span class="token boolean" style="color:#36acaa">true</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__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>
</li>
<li>
<p>Add <code>debugger</code> inside any client code you want debugged. For example,</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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">evaluate</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><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">debugger</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 punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>The Browser will now stop in the location the <code>debugger</code> word is found in
debug mode.</p>
</li>
</ol>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="debugging-methods-for-server-code">Debugging methods for server code<a href="#debugging-methods-for-server-code" class="hash-link" aria-label="Direct link to Debugging methods for server code" title="Direct link to Debugging methods for server code"></a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="use-the-debugger-in-nodejs-chromechromium-only">Use the debugger in Node.js (Chrome/Chromium-only)<a href="#use-the-debugger-in-nodejs-chromechromium-only" class="hash-link" aria-label="Direct link to Use the debugger in Node.js (Chrome/Chromium-only)" title="Direct link to Use the debugger in Node.js (Chrome/Chromium-only)"></a></h3>
<p>Since server code intermingles with client code, this method of debugging is
closely tied with the browser. For example, you can step over
<code>await page.click()</code> in the server script and see the click happen in the
browser.</p>
<p>Note that you won&#x27;t be able to run <code>await page.click()</code> in DevTools console due
to this
<a href="https://bugs.chromium.org/p/chromium/issues/detail?id=833928" target="_blank" rel="noopener noreferrer">Chromium bug</a>, so
if you want to try something out, you have to add it to your test file.</p>
<ol>
<li>
<p>Set <a href="/api/puppeteer.browserlaunchargumentoptions"><code>headless</code></a> to
<code>false</code>.</p>
</li>
<li>
<p>Add <code>debugger</code> to any server code you want debugged. For example,</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token keyword" style="color:#00009f">debugger</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">click</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">&#x27;a[target=_blank]&#x27;</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>
</li>
<li>
<p>Run your server code with <code>--inspect-brk</code>. For example,</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token function" style="color:#d73a49">node</span><span class="token plain"> --inspect-brk path/to/script.js</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>
</li>
<li>
<p>In the opened Chrome/Chromium browser, open <code>chrome://inspect/#devices</code> and
click <code>inspect</code>.</p>
</li>
<li>
<p>In the newly opened test browser, press <code>F8</code> to resume test execution.</p>
</li>
<li>
<p>Now your <code>debugger</code> statement will be hit and you can debug in the test
browser.</p>
</li>
</ol>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="log-devtools-protocol-traffic">Log DevTools protocol traffic<a href="#log-devtools-protocol-traffic" class="hash-link" aria-label="Direct link to Log DevTools protocol traffic" title="Direct link to Log DevTools protocol traffic"></a></h3>
<p>If all else fails, it&#x27;s possible there may be an issue between Puppeteer and the
DevTools protocol. You can debug this by setting the <code>DEBUG</code> environment
variable before running your script. This will log internal traffic via
<a href="https://github.com/visionmedia/debug" target="_blank" rel="noopener noreferrer"><code>debug</code></a> under the <code>puppeteer</code> namespace.</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic"># Basic verbose logging</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token function" style="color:#d73a49">env</span><span class="token plain"> </span><span class="token assign-left variable" style="color:#36acaa">DEBUG</span><span class="token operator" style="color:#393A34">=</span><span class="token string" style="color:#e3116c">&quot;puppeteer:*&quot;</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">node</span><span class="token plain"> script.js</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 comment" style="color:#999988;font-style:italic"># Prevent truncating of long messages</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token function" style="color:#d73a49">env</span><span class="token plain"> </span><span class="token assign-left variable" style="color:#36acaa">DEBUG</span><span class="token operator" style="color:#393A34">=</span><span class="token string" style="color:#e3116c">&quot;puppeteer:*&quot;</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">env</span><span class="token plain"> </span><span class="token assign-left variable" style="color:#36acaa">DEBUG_MAX_STRING_LENGTH</span><span class="token operator" style="color:#393A34">=</span><span class="token plain">null </span><span class="token function" style="color:#d73a49">node</span><span class="token plain"> script.js</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 comment" style="color:#999988;font-style:italic"># Protocol traffic can be rather noisy. This example filters out all Network domain messages</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token function" style="color:#d73a49">env</span><span class="token plain"> </span><span class="token assign-left variable" style="color:#36acaa">DEBUG</span><span class="token operator" style="color:#393A34">=</span><span class="token string" style="color:#e3116c">&quot;puppeteer:*&quot;</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">env</span><span class="token plain"> </span><span class="token assign-left variable" style="color:#36acaa">DEBUG_COLORS</span><span class="token operator" style="color:#393A34">=</span><span class="token plain">true </span><span class="token function" style="color:#d73a49">node</span><span class="token plain"> script.js </span><span class="token operator file-descriptor important" style="color:#393A34">2</span><span class="token operator" style="color:#393A34">&gt;</span><span class="token file-descriptor important">&amp;1</span><span class="token plain"> </span><span class="token operator" style="color:#393A34">|</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">grep</span><span class="token plain"> </span><span class="token parameter variable" style="color:#36acaa">-v</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">&#x27;&quot;Network&#x27;</span><span class="token plain"></span><
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="log-pending-protocol-calls">Log pending protocol calls<a href="#log-pending-protocol-calls" class="hash-link" aria-label="Direct link to Log pending protocol calls" title="Direct link to Log pending protocol calls"></a></h3>
<p>If you encounter issues with async Puppeteer calls not getting resolved, try logging
pending callbacks by using the <a href="https://pptr.dev/api/puppeteer.browser/#properties" target="_blank" rel="noopener noreferrer"><code>debugInfo</code></a> interface
to see what call is the cause:</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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 plain">browser</span><span class="token punctuation" style="color:#393A34">.</span><span class="token plain">debugInfo</span><span class="token punctuation" style="color:#393A34">.</span><span class="token plain">pendingProtocolErrors</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>The getter returns a list of <code>Error</code> objects and the stacktraces of the error objects
indicate which code triggered a protocol call.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="debugging-methods-for-the-browser-code">Debugging methods for the browser code<a href="#debugging-methods-for-the-browser-code" class="hash-link" aria-label="Direct link to Debugging methods for the browser code" title="Direct link to Debugging methods for the browser code"></a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="print-browser-logs">Print browser logs<a href="#print-browser-logs" class="hash-link" aria-label="Direct link to Print browser logs" title="Direct link to Print browser logs"></a></h3>
<p>If the browser unexpectedly crashes or does not launch properly, it could be useful
to inspect logs from the browser process by setting the launch attribute <code>dumpio</code> to <code>true</code>.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-ts codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><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 plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> dumpio</span><span class="token operator" style="color:#393A34">:</span><span class="token plain"> </span><span class="token boolean" style="color:#36acaa">true</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 punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><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_LjdS"><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>In this case, Puppeteer forwards browser logs to the Node process&#x27; stdio.</p></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/guides/configuration"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Configuration</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/guides/network-interception"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Request Interception</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#background" class="table-of-contents__link toc-highlight">Background</a></li><li><a href="#debugging-methods-for-all-situations" class="table-of-contents__link toc-highlight">Debugging methods for all situations</a><ul><li><a href="#turn-off-headless" class="table-of-contents__link toc-highlight">Turn off <code>headless</code></a></li><li><a href="#puppeteer-slow-mo" class="table-of-contents__link toc-highlight">Puppeteer &quot;slow-mo&quot;</a></li></ul></li><li><a href="#debugging-methods-for-client-code" class="table-of-contents__link toc-highlight">Debugging methods for client code</a><ul><li><a href="#capture-console-output" class="table-of-contents__link toc-highlight">Capture <code>console.*</code> output</a></li><li><a href="#use-the-debugger-in-the-browser" class="table-of-contents__link toc-highlight">Use the debugger in the browser</a></li></ul></li><li><a href="#debugging-methods-for-server-code" class="table-of-contents__link toc-highlight">Debugging methods for server code</a><ul><li><a href="#use-the-debugger-in-nodejs-chromechromium-only" class="table-of-contents__link toc-highlight">Use the debugger in Node.js (Chrome/Chromium-only)</a></li><li><a href="#log-devtools-protocol-traffic" class="table-of-contents__link toc-highlight">Log DevTools protocol traffic</a></li><li><a href="#log-pending-protocol-calls" class="table-of-contents__link toc-highlight">Log pending protocol calls</a></li></ul></li><li><a href="#debugging-methods-for-the-browser-code" class="table-of-contents__link toc-highlight">Debugging methods for the browser code</a><ul><li><a href="#print-browser-logs" class="table-of-contents__link toc-highlight">Print browser logs</a></li></ul></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_nPIU"><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_nPIU"><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_nPIU"><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 Googl
</body>
</html>