Puppeteer - page.$$('').length returns und

2020-04-15 11:19发布

问题:

I was having errors with my code, so i tried to log the value in the erroneous code. So i did:

const read = await page.$$('.Ns6lhs9 _gfh3').length;

Then i console.log(read);

For some reason i get undefined although there are elements with class name 'Ns6lhs9 _gfh3' in the HTML

回答1:

$$ returns a promise of an element, while length is not a promise, it's actual value.

It should be:

const read = (await page.$$('.Ns6lhs9._gfh3')).length;


回答2:

I've had a similar issue with getting 0 when counting elements using CSS selector. I tried xpath selector instead and for some reason it did work.