1

I'm attempting to use puppeteer to take a screenshot, and to do this I want to get the coordinates of an element. I'm trying to use this code to do that:

      clip = await page.evaluate(() => {
        global.log('print progress a');
        const uiElement = document.querySelector('#printout');
        global.log('print progress b');
        return uiElement ? uiElement.getBoundingClientRect() : defaultCoordinates;
      });

But as far as I can tell this code isn't even being used; the logs are not written at all and it always uses the defaultCoordinates Can anyone see any reasons why this would not be evaluated?

OliverRadini
  • 6,238
  • 1
  • 21
  • 46
  • Did you try removing anything referring to global variables? – Icepickle Sep 03 '20 at 14:59
  • @Icepickle I think (I hope) I did originally - is puppeteer just doing to evaluate the function in the context of the browser? – OliverRadini Sep 03 '20 at 15:03
  • Yes, it is evaluating it in the browser only afaik, you could have a look at this question: https://stackoverflow.com/questions/46088351/puppeteer-pass-variable-in-evaluate – Icepickle Sep 03 '20 at 15:04
  • @Icepickle that makes sense, then - I'll check that question out, thank you – OliverRadini Sep 03 '20 at 15:05
  • @Icepickle unfortunately it didn't make any difference, still doesn't seem to run evaluate, even when I remove the references to `global.log` and pass `defaultCoorinates` in – OliverRadini Sep 03 '20 at 15:36
  • 1
    I'd probably have a look at using the [`page.waitForElement`](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagewaitforselectorselector-options) and surrounding the await statement with a try/catch to ensure there weren't any errors thrown. The code you show here is rather limited, and can't really be reproduced here that easily – Icepickle Sep 03 '20 at 18:03

0 Answers0