chromedp / cdproto

Package cdproto contains the generated commands, types, and events for the Chrome DevTools Protocol domains.
MIT License
149 stars 54 forks source link

Why do I only get three screenshots using this method? #19

Closed mengjiaheng closed 3 years ago

mengjiaheng commented 3 years ago

Why do I only get three screenshots using this method?

var options = []chromedp.ExecAllocatorOption{ chromedp.Flag("headless", true), chromedp.Flag("hide-scrollbars", false), chromedp.Flag("mute-audio", false), chromedp.UserAgent(Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`), }

func ChromedpTest(url string) {

err := os.Mkdir("./img", os.ModePerm)
if err != nil {
    fmt.Println(err)
}

options = append(chromedp.DefaultExecAllocatorOptions[:], options...)

ctx, _ := chromedp.NewExecAllocator(context.Background(), options...)
ctx, _ = context.WithTimeout(ctx, 20*time.Second)
ctx, cancel := chromedp.NewContext(ctx)
defer cancel()

ListenTarget(ctx)

err = chromedp.Run(ctx, chromedp.Tasks{
    chromedp.Navigate(url),
    chromedp.ActionFunc(func(ctx context.Context) error {
        scr := page.StartScreencast()
        scr.WithFormat("png")
        scr.WithQuality(100)
        scr.WithMaxWidth(1000)
        scr.WithMaxHeight(1000)
        scr.WithEveryNthFrame(18)
        err := scr.Do(ctx)
        if err != nil {
            fmt.Println("do", err)
        }
        return nil
    }),
    chromedp.Sleep(2 * time.Second),
})
if err != nil {
    fmt.Println(err)
}

}

func ListenTarget(ctx context.Context) { n := "1" chromedp.ListenTarget(ctx, func(v interface{}) { switch ev := v.(type) { case *page.EventScreencastFrame: b := Base64Decode(ev.Data) f, _ := os.OpenFile("./img/"+n+"xx.png", os.O_RDWR|os.O_CREATE, os.ModePerm) defer f.Close() f.Write(b) n = n + "1" default: // fmt.Println("hello world") } }) }

func Base64Decode(str string) []byte { decoded, _ := base64.StdEncoding.DecodeString(str) return decoded }`

ZekeLu commented 3 years ago

Is it a duplicate of https://github.com/chromedp/chromedp/issues/920?

If not, please file a new issue over https://github.com/chromedp/chromedp. And please put your source code in Fenced code blocks