25

Is there some way to debug a puppeteer script? One of the buttons just doesn't get clicked for some reason. I've tried all different ways, and actually in another script I get it clicked, but in this one I don't.

await page.focus('#outer-container > nav > span.right > span.search-notification-wrapper > span > form > input[type="text"]');
await page.type("Some text");
await page.click('#outer-container > nav > span.right > span.search-notification-wrapper > span > form'); // I am clicking on the form because it did work in the other script
elena
  • 3,740
  • 5
  • 27
  • 38
  • Hey, we wrote an open source package that really helps with this sort of thing - consider checking us out: https://github.com/testimio/root-cause/ – Benjamin Gruenbaum Sep 24 '20 at 12:58

8 Answers8

57

Kind of a late response, but might be helpful as a reference. You could debug your client script like that:

await page.evaluate(() => {
  debugger;
  const btn = document.querySelector(...);
  btn.click();
});

Now just launch puppeteer using:

puppeteer.launch({devtools: true})

Chromium will open and stop on your breakpoint.

Yaniv Efraim
  • 6,633
  • 7
  • 53
  • 96
10

I found this to be helpful: https://github.com/GoogleChrome/puppeteer#debugging-tips Got this lead from here: https://github.com/GoogleChrome/puppeteer/issues/868

elena
  • 3,740
  • 5
  • 27
  • 38
3

my team wrote an open source library explicitly to help debugging puppeteer. It's very new and I would love feedback.

Basically it does several things you can use in other libraries:

  • It takes screenshots (with page.screenshot)
  • It highlights what elements you interacted with.
  • It collects network data (you can use chrome-har or listen to network events manually for this).
  • It collects console logs (listens to page.console)
  • It interacts with test runners like jest/mocha to collect info from there.
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
0

With async await you can set a breakpoint on the line of code and step into the function call.

node inspect testscript.js

testscript.js

...
await page.focus('#outer-container > nav > span.right > span.search-notification-wrapper > span > form > input[type="text"]');
await page.type("Some text");
debugger;
await page.click('#outer-container > nav > span.right > span.search-notification-wrapper > span > form'); // I am clicking on the form because it did work in the other script
...

This will break on the page.click call and then we can step into with step or s command on the debugger.

This of course is made real convenient by IDEs like Visual Studio Code.

nilobarp
  • 3,806
  • 2
  • 29
  • 37
  • Thanks! Any idea why it breaks at the very beginning of the script even if I don't put any breakpoint there? – elena Sep 25 '17 at 07:53
  • Use `node --debug` instead of `inspect` if you don't want the debugger to stop at first line. – nilobarp Sep 25 '17 at 07:57
  • 1
    apparently, it's node --inspect, since --debug option is deprecated. – elena Sep 25 '17 at 08:02
  • It seems that await doesn't work in the debugger. I am wondering if I could use the code in same exact way as in the script itself? For example, if I want to see what does exactly `await page.click('#outer-container > nav > span.right > span.search-notification-wrapper > span > form');` do? – elena Sep 25 '17 at 08:18
  • 1
    @elena - a little late response, but you could do something like: `await page.evaluate(() => {debugger;const btn = document.querySelector(...);btn.click();});` And then `puppeteer.launch({devtools: true})` – Yaniv Efraim Apr 17 '18 at 20:45
0

If you're looking for something interactive, I have a Github repo/docker image that ships with an interactive debugger which makes debugging at the browser level a lot easier since you both visually see what's going on and inspect the page itself. I've found that doing debugging in your node/puppeteer script really isn't valuable as all the action is taking place on the remote browser.

Github repo is here, docker repo is here.

browserless
  • 2,090
  • 16
  • 16
0

Printing console messages might be helpful:

page.on('console', msg => console.log('PAGE LOG:', msg.text()))
-1

with intellij webstorm is very easy, because it integrate well with puppeteer and node and you can simply but a breakpoint on the left side without configure anything else. you can also obtain the same result with visual studio code, that is free, but you need to configure launch.json, and this is pretty hard (so i moved to webstorm).

Andrea Bisello
  • 1,077
  • 10
  • 23
-2

Use Visual Studio code. You can set up a break point by simply clicking on left bar in text editor - https://code.visualstudio.com/docs/nodejs/nodejs-debugging