Swastik
Swastik

Reputation: 396

How to find all child nodes of a node in puppeteer

Is there any way in puppeteer to find all child nodes of a node. In Java Selenium WebDriver we can find all child elements of a WebElement as shown here:

WebElement parent = driver.findElement(By.xpath("..."));
List<WebElement> children = parent.findElements(By.tagName("div"));

Now is there any similar methods in puppeteer to fetch child elements like this. If i'm fetching parent element/node like this

var parent = await page.$x('//*[@class="myClass"]');

The problem is the above line of code returns list of nodes, now i have to find child nodes of each node in parent array.

for example:

var child = parent[0].childrens;

Something like this

Upvotes: 2

Views: 1472

Answers (1)

Prophet
Prophet

Reputation: 33361

To get all the child elements of element located by //*[@class="myClass"] XPath you can simply do this:

var children = await page.$x('//*[@class="myClass"]//*');

Upvotes: 2

Related Questions