Created
June 4, 2019 06:34
-
-
Save coffee-mug/37f62e1a788fde25c76ce2b691d6ce0f to your computer and use it in GitHub Desktop.
Get all cookies with Golang chromedp
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" | |
"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