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

对着背影说爱祢 提交于 2019-12-02 12:36:41

问题


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.



来源:https://stackoverflow.com/questions/50704495/puppeteer-page-length-returns-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!