Using chromedp in Go

Hossein MarganiHossein Margani
2 min read

chromedp is a powerful and lightweight library for controlling headless Chrome or Chromium instances using the CDP (Chrome DevTools Protocol) from within Go applications. It offers a clean, idiomatic Go API for automating browser interactions, making it ideal for tasks like web scraping, testing, and generating screenshots.

Getting Started

To begin, you'll need to install the chromedp package. You can use Go modules for this:

go get github.com/chromedp/chromedp

Next, let's explore a basic example to demonstrate the core functionality. The following code snippet demonstrates how to navigate to a URL and get the page title:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/chromedp/chromedp"
)

func main() {
    opts := append(chromedp.DefaultOptions(), chromedp.DisableGPU)
    ctx, cancel := chromedp.NewContext(context.Background(), opts...)
    defer cancel()

    var title string
    err := chromedp.Run(ctx, 
        chromedp.Navigate(`https://www.example.com`),
        chromedp.Text(`body > title`, &title, chromedp.ByQuery),
    )

    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Title: %s\n", title)
}

This is a simple example of opening a new browser, navigating to a page, selecting an element, and extracting text from the page. chromedp offers advanced features such as handling user interactions, managing cookies, and taking screenshots. Further exploration of this example shows a more complex scenario.

Practical Application

A common use case for chromedp is web scraping. By automating browser actions, you can efficiently extract data from websites that are difficult or impossible to scrape using simple HTTP requests. For instance, websites with JavaScript-rendered content or those using dynamic loading techniques are ideal candidates for chromedp.

Further Reading

The official chromedp repository on GitHub provides comprehensive documentation, tutorials, and examples to help you master this powerful library. The project's README contains a wealth of information to enhance your understanding and allow you to tackle more advanced use cases.

0
Subscribe to my newsletter

Read articles from Hossein Margani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hossein Margani
Hossein Margani