chromedp/_example/simple.go
Daniel Martí 3d3bf22ccc start the chromedp v2 refactor
First, we want all of the functionality in a single package; this means
collapsing whatever is useful into the root chromedp package.

The runner package is being replaced by the Allocator interface, with a
default implementation which starts browser processes.

The client package doesn't really have a place in the new design. The
context, allocator, and browser types will handle the connection with
each browser.

Finally, the new API is context-based, hence the addition of context.go.
The tests have been modified to build and run against the new API.
2019-04-01 12:17:28 +01:00

37 lines
647 B
Go

package main
import (
"context"
"fmt"
"log"
"github.com/chromedp/chromedp"
)
func main() {
// create a new context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// grab the title
var title string
if err := chromedp.Run(ctx, grabTitle(&title)); err != nil {
log.Fatal(err)
}
// print it
fmt.Println(title)
// ensure all resources are cleaned up
cancel()
chromedp.FromContext(ctx).Wait()
}
func grabTitle(title *string) chromedp.Tasks {
return []chromedp.Action{
chromedp.Navigate("https://github.com/"),
chromedp.WaitVisible("#start-of-content", chromedp.ByID),
chromedp.Title(title),
}
}