简体   繁体   English

如何使用puppeteer获取html元素的所有子元素值

[英]How to get all the child elements values of an html element using puppeteer

I am using puppeteer . 我正在使用木偶戏 I want to get the values of column names of a table. 我想获取表的列名的值。

<tbody>
<tr class="GridHeader" align="center" style="background-color:Black;">
    <td class="HeaderStyleOfdatalist">XYZ</td>
    <td>0500</td>
    <td>0550</td>
    <td>0600</td>
    <td>0650</td>
</tr>
</tbody>

What I need to get is an array of these td values. 我需要得到的是这些td值的数组。 I tried page.$(selector) but could not understand the output. 我试过页面。$(选择器)但无法理解输出。 I also tried: 我也尝试过:

let idAttribute = await page.$eval('.GridHeader', e => e.childNodes);
console.log(idAttribute)

But not able to get the array of these td values. 但是无法获得这些td值的数组。

Can you please help me loop over these values. 你能不能请我帮我循环这些价值观。

EDIT : Found the answer to the problem and posted it in the answers section. 编辑 :找到问题的答案并将其发布在答案部分。

I was able to get the solution using: 我能够使用以下方法获得解决方案:

const data = await page.evaluate(() => {
        const tds = Array.from(document.querySelectorAll('.GridHeader td'))
        return tds.map(td => td.textContent)
    });

console.log(data) // ['xyz', '0500', '0550','0600', '0650']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用puppeteer获取HTML元素的子元素,以便与其浏览器计算的样式一起创建HTML树? - How can I get child elements of a HTML element using puppeteer in order to create a HTML tree along with their browser computed styles? 如何使用 puppeteer 获取 HTML 元素文本 - How to get HTML element text using puppeteer 如何获取 HTML 元素但使用 querySelector 排除所有子元素 - How to get the HTML element but exclude all child elements with querySelector 如何在 puppeteer 中获取列表中的所有元素 - How to get all elements in a list in puppeteer 如何使用 puppeteer 从 n 个子元素中检索属性值? - How to retrieve attribute values from n number of child elements using puppeteer? 当页面加载框架集标记内的所有元素时,如何使用 puppeteer 获取输入元素 - How to get input element with puppeteer, when the page load all elements inside frameset tag 量角器如何从父元素中获取所有子元素 - Protractor how to get all child elements from parent element 如何使用javascript和jquery从html元素到特定的html元素字段分别获取值 - How to get values separately from html elements to specfic html element field using javascript and jquery 如何使用AngularJS获取子HTML元素的属性值 - How do I get Child HTML elements attribute values using AngularJS 如何使用JS从div元素中删除所有子元素 - How to delete all Child Elements from a div element using JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM