Debian Codegraph @

lib/examples/compare-chromedp/screenshot/main.go

raw
1 // Package main ...
2 package main
3
4 import (
5 "os"
6
7 "github.com/go-rod/rod"
8 "github.com/go-rod/rod/lib/proto"
9 "github.com/ysmood/gson"
10 )
11
12 // This example demonstrates how to take a screenshot of a specific element and
13 // of the entire browser viewport, as well as using `kit`
14 // to store it into a file.
15 func main() {
16 browser := rod.New().MustConnect()
17
18 // capture screenshot of an element
19 browser.MustPage("https://google.com").MustElement("body div").MustScreenshot("elementScreenshot.png")
20
21 // capture entire browser viewport, returning jpg with quality=90
22 buf, err := browser.MustPage("https://brank.as/").Screenshot(true, &proto.PageCaptureScreenshot{
23 Format: proto.PageCaptureScreenshotFormatJpeg,
24 Quality: gson.Int(90),
25 })
26 if err != nil {
27 panic(err)
28 }
29
30 err = os.WriteFile("fullScreenshot.png", buf, 0o644)
31 if err != nil {
32 panic(err)
33 }
34 }