Chromedp: help, how to get all subnode in a element

Created on 1 Feb 2017  路  3Comments  路  Source: chromedp/chromedp

I want to get all subnode in a element. use above code

func search(nodes *[]*cdptypes.Node) cdp.Tasks {

    return cdp.Tasks{
        cdp.Navigate(`https://www.****.com`),
        cdp.Sleep(2 * time.Second),
        cdp.Nodes(`#lt-center > #MOP > #odds-tbl-containers > #sc1 > #s1`, nodes),
        cdp.ActionFunc(func(context.Context, cdptypes.FrameHandler) error {
            return nil
        }),
    }
}

the nodes only have one element, the node.ChildNodeCount = 30 but, not have any sub node in node.Children

All 3 comments

You just need to define it in the selectors.
For example, look at the selector i write bellow. It will tell chromedp to fetch all a elements which a child of ul[class="list-unstyled"] > li

var nodes []*cdptypes.Node
t := chromedp.Tasks{
    chromedp.Navigate(`https://godoc.org`),
    chromedp.Sleep(time.Second * 2),
    chromedp.Nodes(`ul[class="list-unstyled"] > li > a`, &nodes, chromedp.ByQueryAll),
}

err = c.Run(ctx, t)
if err != nil {
    log.Fatal(err)
}

for _, n := range nodes {
    fmt.Printf("got package: %s \n", n.AttributeValue("href"))
}

Just be creative with your selectors.

@ranch thanks!

How can we do this with XPath selector?
We have byQuery and byQueryAll, there is also bySearch (which is working with XPath) but there is no bySearchAll.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aymericbeaumet picture aymericbeaumet  路  5Comments

guotie picture guotie  路  3Comments

donfrigo picture donfrigo  路  4Comments

mschilli picture mschilli  路  4Comments

jafriyie1 picture jafriyie1  路  3Comments