Chromedp: Generate the rendered html as string

Created on 16 May 2017  路  3Comments  路  Source: chromedp/chromedp

Hi,

I was wondering if it is possible to get the complete rendered html for a given website.

Thanks.

Most helpful comment

You can get the root node after the html is rendered and use it to get the html.

Chrome.Run(ctx, cdp.ActionFunc(func(ctxt context.Context, h cdptypes.Handler) error {
            var err error
            root, err := h.GetRoot(ctxt)
            if err != nil {
                log.Errorf("Failed to get root node: %v", err)
                return err
            }

            html, err = dom.GetOuterHTML(root.NodeID).Do(ctxt, h)
            if err != nil {
                log.Errorf("Failed to get outer HTML: %v", err)
                return err
            }
            log.Debugf("Outer HTML: %#v", html)
            return nil
        }))

All 3 comments

You can get the root node after the html is rendered and use it to get the html.

Chrome.Run(ctx, cdp.ActionFunc(func(ctxt context.Context, h cdptypes.Handler) error {
            var err error
            root, err := h.GetRoot(ctxt)
            if err != nil {
                log.Errorf("Failed to get root node: %v", err)
                return err
            }

            html, err = dom.GetOuterHTML(root.NodeID).Do(ctxt, h)
            if err != nil {
                log.Errorf("Failed to get outer HTML: %v", err)
                return err
            }
            log.Debugf("Outer HTML: %#v", html)
            return nil
        }))

As @af608 pointed out, this is the proper way to get the outer HTML. I'll make a point to add a high-level action to get the inner and outer HTML of an element, however.

OuterHTML and InnerHTML high level actions have been added in 1a35cceeae17ddbf999b586d4e1770963eedc710.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AmrAlfoly picture AmrAlfoly  路  3Comments

donfrigo picture donfrigo  路  4Comments

Tallone picture Tallone  路  4Comments

shailendrayadav1 picture shailendrayadav1  路  4Comments

botsphp picture botsphp  路  3Comments