admin管理员组

文章数量:1429130

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

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

Share Improve this question asked Jun 5, 2018 at 16:03 user1584421user1584421 3,90312 gold badges57 silver badges98 bronze badges 1
  • Possible duplicate of Accessing the await-ed value inline (JS)? – Heretic Monkey Commented Jun 5, 2018 at 16:19
Add a ment  | 

2 Answers 2

Reset to default 5

$$ 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;

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.

本文标签: javascriptPuppeteerpage(3939)length returns undefinedStack Overflow