question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to get element childNodes/children

See original GitHub issue
var listSelector = "#list"
var listElement = await page.$(listSelector);
var listChildren = await page.evaluateHandle(e => e.children, listElement);
// listChildren is null here

I’ve tried dozens of other ways, including using elementHandle.getProperties as described here, and I can’t get a list of children.

My end goal is to get a list, iterate through all the children, and then click on one of the children based on its text. I can do the text clicking part, but I’m having a heck of a time getting node children.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

61reactions
willy2dgcommented, Jul 10, 2020

A bit late, but just in case it’s still useful or someone comes here from Google. You can use the ‘:scope’ selector.

let $parent = await page.$('#parent')
let $childs = await $parent.$$(':scope > *')
2reactions
vsemozhetbytcommented, Aug 19, 2019

To iterate over children, you have at least 2 options: 1) transfer all the iteration logic to the browser context and get the final data; 2) get an array of JSHandles to iterate over it:

const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch();
    const [page] = await browser.pages();

    await page.goto('https://example.org/');

    // Way 1.
    const childrenNames = await page.evaluate((selector) => {
      const names = [];
      for (element of document.querySelector(selector).children) {
        names.push(element.tagName);
      }
      return names;
    }, 'body div');
    console.log(childrenNames);

    // Way 2.
    const elements = await page.$$('body div > *');
    const names = [];
    for (const element of elements) {
      names.push(await (await element.getProperty('tagName')).jsonValue());
    }
    console.log(names);

    await browser.close();
  } catch (err) {
    console.error(err);
  }
})();
Read more comments on GitHub >

github_iconTop Results From Across the Web

can't access child node's value but i ...
Actually nodeType 1 is an HTML element, i.e. div, span etc and an element doesn't has any nodeValue instead it could contain child...
Read more >
Node.childNodes - Web APIs - MDN Web Docs
To get a collection containing only elements, use Element.children instead. Value. A live NodeList containing the children of the node. Note: ...
Read more >
Element (jsoup Java HTML Parser 1.15.3 API)
A HTML element consists of a tag name, attributes, and child nodes (including text ... Get the number of child nodes of this...
Read more >
Parsing XML to get child nodes - apex
querySelectorAll function, XmlNode.getChildElement() and so on doesn't recursively find child elements. <UpdateResponse> isn't the immediate ...
Read more >
Locating child nodes of WebElements in selenium.
First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found