NEWStandard createTask and in.php APIs. Change the address and you are connected.View API docs
GATESOLVER

Web scraping

How do I debug Puppeteer scripts?

Make the browser visible and slow, bring the page's console and network events into your terminal, and save a screenshot and the HTML whenever a step fails.

Start by watching it

Most Puppeteer bugs become obvious when you can see the browser. Launch with headless: false, add slowMo to slow every action, and open devtools automatically. You will see the cookie banner that covers your button, the login redirect, or the CAPTCHA that appeared instead of the page you expected.

import pptrDebug from "puppeteer";

const debugBrowser = await pptrDebug.launch({
  headless: false,
  slowMo: 120,          // ms added to each action
  devtools: true,       // opens devtools for each tab
  defaultViewport: { width: 1440, height: 960 },
});
const tab = await debugBrowser.newPage();
tab.setDefaultTimeout(15000);

Bring the page's logs to your terminal

Errors inside the page do not show up in Node by default. Listen for them:

tab.on("console", (msg) => console.log("[page]", msg.type(), msg.text()));
tab.on("pageerror", (err) => console.log("[page error]", err.message));
tab.on("requestfailed", (req) => console.log("[failed]", req.url(), req.failure()?.errorText));
tab.on("response", (res) => {
  if (res.status() >= 400) console.log("[http]", res.status(), res.url());
});

A 403 on the main document, a 429 on an API call, or a failed script load usually explains a later timeout.

Save evidence on failure

Wrap each important step so that a failure leaves a screenshot and the HTML behind. In headless runs on a server, this is your only window into what happened.

import { writeFile } from "node:fs/promises";

async function step(name, fn) {
  try {
    return await fn();
  } catch (err) {
    const stamp = Date.now();
    await tab.screenshot({ path: `fail-${name}-${stamp}.png`, fullPage: true });
    await writeFile(`fail-${name}-${stamp}.html`, await tab.content());
    console.error(`step ${name} failed:`, err.message);
    throw err;
  }
}

The HTML file shows the DOM as it was at the moment of failure, including any overlay or error message, and you can open it next to the screenshot. The screenshot guide lists more options.

Common causes and their fixes

  • Timeouts on selectors. The element never appeared, or appeared inside an iframe. Use page.frames() to find it, and wait for the right condition, as described in waiting for page load.
  • Clicks that do nothing. Something covers the element, or it is outside the viewport. Scroll it into view and check for overlays.
  • Works headful, fails headless. The site treats headless browsers differently. Compare screenshots from both modes; a block page or challenge in headless points to headless detection.
  • Navigation races. Clicking a link and waiting separately can miss the navigation. Start the wait first, then click, inside Promise.all.

Debugging CAPTCHA steps

When a challenge appears, log its parameters before you solve: the site key, the page URL, whether the widget is inside an iframe, and any callback name. Then log the task id, the time the answer became ready, and what the page did after you injected the token. If the token is accepted but the form does not submit, the site probably expects a callback rather than a filled field. The guide on Puppeteer being detected covers why some pages keep showing the check.

Step through with the Node inspector

For logic bugs in your own code, run node --inspect-brk script.mjs and attach Chrome's inspector at chrome://inspect. Breakpoints in your script pause the automation, and you can poke at the live page in the visible browser while it waits. Combined with slowMo, this turns a flaky timeout into a reproducible, inspectable moment.

Keep a small reproduction

When a bug only shows on one site, cut the script down to the fewest steps that still fail: launch, open the URL, perform the one action, log the result. A short reproduction runs in seconds, so you can try ten ideas in the time one full run takes, and it is far easier to share with a colleague.

Common questions

How do I see console.log from inside page.evaluate?

Listen to the page console event; messages logged in the page are forwarded to your handler.

Why does my script pass headful and fail headless?

Timing and detection differ. Compare screenshots from both modes and check for block pages in headless.

What does slowMo do?

It adds a delay in milliseconds before each Puppeteer action, so you can follow what the script does.

How do I debug elements inside an iframe?

Find the frame with page.frames() or elementHandle.contentFrame(), then query inside that frame.

Should I keep debug listeners in production?

Keep the failure screenshots and error logs; drop verbose console forwarding to save disk and noise.

Keep reading