$ 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
I followed the example for screenshot in examples folder for taking a screenshot of specific element.
I expected to see a fully image of the element.
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:

What am I doing wrong?
Note: You can not see the image because it is white...
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.
Most helpful comment
Same issue for me. In headless mode
Screenshotmethod doesn't work.upd: I noticed that if you set WindowSize, everything starts working as it should.