Chromedp: Screenshot issue

Created on 14 Mar 2020  路  2Comments  路  Source: chromedp/chromedp

What versions are you running?

$ go list -m github.com/chromedp/chromedp - github.com/chromedp/chromedp v0.5.3
$ google-chrome --version - Google Chrome 80.0.3987.132
$ go version - go version go1.13.8 darwin/amd64

What did you do? Include clear steps.

I followed the example for screenshot in examples folder for taking a screenshot of specific element.

What did you expect to see?

I expected to see a fully image of the element.

What did you see instead?

Sometimes the screenshot is cut off - like I saw the few lines of pixels but the rest was blanked.
And sometimes I see only a blank image.

this is the code:

// TakeScreenShot - screenshot
func TakeScreenShot() {
    // create context
    ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()

    // capture screenshot of an element
    var buf []byte
    if err := chromedp.Run(ctx, elementScreenshot(`https://www.ynet.co.il/home/0,7340,L-8,00.html`, `[data-vr-zone='Strip 2']`, &buf)); err != nil {
        log.Fatal(err)
    }
    if err := ioutil.WriteFile("elementScreenshot.png", buf, 0644); err != nil {
        log.Fatal(err)
    }
}

// elementScreenshot takes a screenshot of a specific element.
func elementScreenshot(urlstr, sel string, res *[]byte) chromedp.Tasks {
    return chromedp.Tasks{
        chromedp.Navigate(urlstr),
        chromedp.WaitVisible(sel, chromedp.ByQuery),
        chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByQuery),
    }
}

This is what I get:
elementScreenshot

What am I doing wrong?

Note: You can not see the image because it is white...

question

Most helpful comment

Same issue for me. In headless mode Screenshot method doesn't work.

upd: I noticed that if you set WindowSize, everything starts working as it should.

All 2 comments

update:

if I invoked the new context like that it works:

...
...
...
allocCtx, cancel := chromedp.NewExecAllocator(context.Background())
    defer cancel()

ctx, cancel := chromedp.NewContext(context.Background())
    defer cancel()
...
...
...

any idea why?

Same issue for me. In headless mode Screenshot method doesn't work.

upd: I noticed that if you set WindowSize, everything starts working as it should.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jafriyie1 picture jafriyie1  路  3Comments

opennota picture opennota  路  4Comments

donfrigo picture donfrigo  路  4Comments

guotie picture guotie  路  3Comments

pmurley picture pmurley  路  5Comments