Last active
December 23, 2025 14:12
-
-
Save geberl/186bb0ed05b972b69292ef865ebfdbaa to your computer and use it in GitHub Desktop.
`brew install --cask google-chrome`, `go run ./... `
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "log" | |
| "os" | |
| "github.com/chromedp/chromedp" | |
| "github.com/go-rod/rod" | |
| "github.com/go-rod/rod/lib/launcher" | |
| "github.com/tebeka/selenium" | |
| ) | |
| func main() { | |
| urlToCapture := "http://example.com/page.html" | |
| // buf, err := localChrome(urlToCapture) | |
| // buf, err := remoteSeleniumWebdriverRod(urlToCapture) | |
| buf, err := remoteSeleniumWebdriverTebeka(urlToCapture) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| if err := os.WriteFile("screenshot.png", buf, 0644); err != nil { | |
| log.Fatal(err) | |
| } | |
| } | |
| func localChrome(url string) ([]byte, error) { | |
| chromeLocation := "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" | |
| opts := append(chromedp.DefaultExecAllocatorOptions[:], | |
| chromedp.ExecPath(chromeLocation), | |
| chromedp.WindowSize(800, 480), | |
| ) | |
| allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) | |
| defer cancel() | |
| ctx, cancel := chromedp.NewContext(allocCtx) | |
| defer cancel() | |
| var buf []byte | |
| if err := chromedp.Run(ctx, | |
| chromedp.Navigate(url), | |
| chromedp.FullScreenshot(&buf, 90), // 90 is quality | |
| ); err != nil { | |
| return nil, err | |
| } | |
| return buf, nil | |
| } | |
| func remoteSeleniumWebdriverRod(url string) ([]byte, error) { | |
| // chromedp doesn't natively support Selenium's WebDriver protocol - it expects direct Chrome DevTools Protocol (CDP) access. | |
| // You'd need to manually create a session first. | |
| // | |
| // The rod library apparently has better Selenium support out of the box. | |
| // Unfortunately, neither chromedp nor rod work well with Selenium Grid out of the box because they expect direct CDP access, not the WebDriver protocol. | |
| wsURL := "ws://webdriver.example.com:4444/wd/hub" | |
| l := launcher.MustResolveURL(wsURL) | |
| browser := rod.New().ControlURL(l).MustConnect() | |
| defer browser.MustClose() | |
| page := browser.MustPage(url).MustSetViewport(800, 480, 1, false) | |
| buf := page.MustWaitLoad().MustScreenshot() | |
| return buf, nil | |
| } | |
| func remoteSeleniumWebdriverTebeka(url string) ([]byte, error) { | |
| wdURL := "https://webdriver.example.com:4444/wd/hub" | |
| caps := selenium.Capabilities{ | |
| "browserName": "chrome", | |
| } | |
| wd, err := selenium.NewRemote(caps, wdURL) | |
| if err != nil { | |
| return nil, err | |
| } | |
| defer wd.Quit() | |
| if err := wd.ResizeWindow("", 800, 480); err != nil { | |
| return nil, err | |
| } | |
| if err := wd.Get(url); err != nil { | |
| return nil, err | |
| } | |
| screenshot, err := wd.Screenshot() | |
| if err != nil { | |
| return nil, err | |
| } | |
| return screenshot, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment