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
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.