Skip to content

Instantly share code, notes, and snippets.

@coffee-mug
Created June 4, 2019 06:34
Show Gist options
  • Save coffee-mug/37f62e1a788fde25c76ce2b691d6ce0f to your computer and use it in GitHub Desktop.
Save coffee-mug/37f62e1a788fde25c76ce2b691d6ce0f to your computer and use it in GitHub Desktop.
Get all cookies with Golang chromedp
package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
func main() {
var err error
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// get those yummy cookies
err = chromedp.Run(ctx,
chromedp.Navigate("https://www.renault.fr"),
chromedp.WaitVisible("container", chromedp.ByID),
chromedp.ActionFunc(func(ctx context.Context) error {
cookies, err := network.GetAllCookies().Do(ctx)
var c []string
for _, v := range cookies {
aCookie := v.Name + " - " + v.Domain
c = append(c, aCookie)
}
stringSlices := strings.Join(c[:], ",\n")
fmt.Printf("%v", stringSlices)
if err != nil {
return err
}
return nil
}),
)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment