puppeteer: Getting HTML from NodeList?

≯℡__Kan透↙ 提交于 2020-12-26 09:12:36

问题


I'm getting a list of 30 items from the code:

const boxes = await page.evaluate(() => {
    return document.querySelectorAll("DIV.a-row.dealContainer.dealTile")
})

console.log(boxes);

The result

{ '0': {},
  '1': {},
  '2': {},
  ....
  '28': {},
  '29': {} }

I have the need to see the html of the elements.

But every property I tried of boxes is simply undefined. I tried length, innerHTML, 'innerText` and some other.

I am sure of box really containing something because puppeteer's screenshot shows the content before I start to 'browse' the content of the page

What am I doing wrong?


回答1:


There are multiple ways to do this:

  • Use page.$$eval to execute the selector and return the result in one step.
  • Use page.evaluate to get the attributes after querying the elements.

Code sample for page.$$eval

const htmls = await page.$$eval('selector', el => el.innerHTML);

Code sample for page.evaluate

const singleBox = boxes[0];
const html = await page.evaluate(el => el.innerHTML, singleBox);


来源:https://stackoverflow.com/questions/61697605/puppeteer-getting-html-from-nodelist

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